//
// Copyright 2006 GoneGothic.com
//

(function($){var textarea,staticOffset;var iLastMousePos=0;var iMin=32;var grip;$.fn.TextAreaResizer=function(){return this.each(function(){textarea=$(this).addClass('processed'),staticOffset=null;$(this).wrap('<div class="resizable-textarea"><span></span></div>').parent().append($('<div class="grippie"></div>').bind("mousedown",{el:this},startDrag));var grippie=$('div.grippie',$(this).parent())[0];grippie.style.marginRight=(grippie.offsetWidth-$(this)[0].offsetWidth)+'px'})};function startDrag(e){textarea=$(e.data.el);textarea.blur();iLastMousePos=mousePosition(e).y;staticOffset=textarea.height()-iLastMousePos;textarea.css('opacity',0.25);$(document).mousemove(performDrag).mouseup(endDrag);return false}function performDrag(e){var iThisMousePos=mousePosition(e).y;var iMousePos=staticOffset+iThisMousePos;if(iLastMousePos>=(iThisMousePos)){iMousePos-=5}iLastMousePos=iThisMousePos;iMousePos=Math.max(iMin,iMousePos);textarea.height(iMousePos+'px');if(iMousePos<iMin){endDrag(e)}return false}function endDrag(e){$(document).unbind('mousemove',performDrag).unbind('mouseup',endDrag);textarea.css('opacity',1);textarea.focus();textarea=null;staticOffset=null;iLastMousePos=0}function mousePosition(e){return{x:e.clientX+document.documentElement.scrollLeft,y:e.clientY+document.documentElement.scrollTop}}})(jQuery);

(function($) {

	$.ggbox = function(title, data, action) {
		$.ggbox.init();
		$.ggbox.loading();
		$.ggbox.reveal(title, data, action);
	}

	$.ggbox.init = function() {
		$('body').append($.ggbox.defaults.html);
	}

	$.ggbox.loading = function() {
		if ($('#ggbox .loading').length == 1) return true

		$('#ggbox .title1').empty();
		$('#ggbox .title1').append('Loading...');
		$('#ggbox .content').empty()
		$('#ggbox .footer').empty()
		$('#ggbox .body').children().hide().end().append('<div class="loading"><img src="' + webroot + '/images/ggbox/loading.gif"></div>')

		var pageScroll = $.ggbox.getPageScroll();
		$('#ggbox').css({
			top:	pageScroll[1] + ($.ggbox.getPageHeight() / 10),
			left:	pageScroll[0]
		}).show();

		$('#ggbox-overlay').css({
			height: $(document).height() + 'px'
		}).show();

		$(document).bind('keydown.ggbox', function(e) {
			if (e.keyCode == 27) $.ggbox.close();
		})
	}

	$.ggbox.reveal = function(title, data, action) {
		$('#ggbox .title1').empty();
		$('#ggbox .title1').append(title);
		$('#ggbox .content').append(data);
		$('#ggbox .footer').append(action);
		$('#ggbox .loading').remove();
		$('#ggbox .body').children().show();
	}

	$(document).bind('close.ggbox', function() {
		$(document).unbind('keydown.ggbox');
		$('#ggbox-overlay').hide();
		$('#ggbox').hide();
		$('#ggbox .content').removeClass().addClass('content');
		$('#ggbox-overlay').remove();
		$('#ggbox').remove();
	})

	$.ggbox.close = function() {
		$(document).trigger('close.ggbox');
		return false
	}

	$.ggbox.getPageScroll = function() {
		var xScroll, yScroll;
		if (self.pageYOffset) {
			yScroll = self.pageYOffset;
			xScroll = self.pageXOffset;
		} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
			yScroll = document.documentElement.scrollTop;
			xScroll = document.documentElement.scrollLeft;
		} else if (document.body) {// all other Explorers
			yScroll = document.body.scrollTop;
			xScroll = document.body.scrollLeft;
		}
		return new Array(xScroll,yScroll)
	}

	// adapter from getPageSize() by quirksmode.com
	$.ggbox.getPageHeight = function() {
		var windowHeight
		if (self.innerHeight) {	// all except Explorer
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowHeight = document.body.clientHeight;
		}
		return windowHeight
	}

	$.ggbox.debug = function (data) {
		if (window.console && window.console.log)
			window.console.log(data);
	};

	$.ggbox.defaults = {
		html: '\
			<div id="ggbox-overlay" style="display:none;"></div> \
			<div id="ggbox" style="display:none;"> \
				<div class="popup"> \
					<table> \
						<tr> \
							<td class="tl"/><td class="b"/><td class="tr"/> \
						</tr> \
						<tr> \
							<td class="b"/> \
							<td style="padding: 0;"> \
								<div class="title1"></div> \
								<div  class="body"> \
									<div class="content"></div> \
									<div class="footer"></div> \
								</div> \
							</td> \
							<td class="b"/> \
						</tr> \
						<tr> \
							<td class="bl"/><td class="b"/><td class="br"/> \
						</tr> \
					</table> \
				</div> \
			</div>'
	}

})(jQuery);


jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') {
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString();
        }
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else {
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

function updateCharCount(form, element, counter, max) {
	var f = document.forms[form];
	var e = f.elements;
	var old = e[counter].value;
	var sl = e[element].value.length;
	var overflow = sl - max;

	e[counter].value = e[element].value.length;

	if (e[counter].value > max && old <= max) {
		alert("Your signature cannot be longer than " + max + " characters!\nPlease shorten your signature by " + overflow + " characters.");
		if (document.styleSheets) {
			e[counter].style.color = '#ff0000';
		}
	} else if (e[counter].value <= max && old > max && document.styleSheets) {
		e[counter].style.color = '#FFFFFF';
	}
}

function submitSignature(form, element, max) {
	var f = document.forms[form];
	var e = f.elements;
	var ol = e[element].value.length;
	var overflow = ol - max;
	if (ol > max) {
		alert("Your signature cannot be longer than " + max + " characters!\nPlease shorten your signature by " + overflow + " characters.");
		e[element].focus();
		return false;
	}
}

function selectSwitch(form, action) {
	var f = document.forms[form];
	var e = f.elements;
	for (var i = 0; i < e.length; i++) {
		if (e[i].type != 'checkbox') { continue; }
		e[i].checked = action;
	}
}

function submitOnce(form) {
	var f = document.forms[form];
	var e = f.elements;
	for (var i = 0; i < e.length; i++) {
		if (!/submit|image/.test(e[i].type)) { continue; }
		e[i].disabled = true;
		e[i].value = 'Please wait...';
	}
}

function openWindow(url, name, features) {
	window.open(url, name, features);
}

function storeCaret(element) {
	if (element.createTextRange){
		element.caretPos = document.selection.createRange().duplicate();
	}
}

function emoticon(form, element, text) {
	var f = document.forms[form];
	var e = f.elements;
	text = ' ' + text + ' ';
	if (e[element].createTextRange && e[element].caretPos) {
		var caretPos = e[element].caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? caretPos.text + text + ' ' : caretPos.text + text;
		e[element].focus();
	} else {
		var selStart = e[element].selectionStart;
		var selEnd = e[element].selectionEnd;
		mozWrap(e[element], text, '')
		e[element].focus();
		e[element].selectionStart = selStart + text.length;
		e[element].selectionEnd = selEnd + text.length;
	}
}

function mozWrap(txtarea, open, close) {
	var selLength = txtarea.textLength;
	var selStart = txtarea.selectionStart;
	var selEnd = txtarea.selectionEnd;
	var scrollTop = txtarea.scrollTop;
	if (selEnd == 1 || selEnd == 2) {
		selEnd = selLength;
	}
	var s1 = (txtarea.value).substring(0, selStart);
	var s2 = (txtarea.value).substring(selStart, selEnd)
	var s3 = (txtarea.value).substring(selEnd, selLength);
	txtarea.value = s1 + open + s2 + close + s3;
	txtarea.selectionStart = selEnd + open.length + close.length;
	txtarea.selectionEnd = txtarea.selectionStart;
	txtarea.focus();
	txtarea.scrollTop = scrollTop;
	return;
}

var current_tab = 1;
var fb_delete_comment = new Array('Delete Comment', 'Are you sure you want to delete this comment?');

$(document).ready(function(){
	var cookie_tab = $.cookie("ob");
	var initial_tab = (null !== cookie_tab) ? cookie_tab : 1;
	switchTab('ob', 4, initial_tab);

	$('#lockip').attr('checked', Boolean(($.cookie('lock') == "false") ? "" : "true"));
	$("#lockip").change(function(){ $.cookie('lock', this.checked, { expires: 30 }) });
	$("#top").click(function(){ $("html, body").animate({ scrollTop: 0 }, "slow");  return false; });
	$('textarea:not(.processed)').TextAreaResizer();
});

function delete_comment(url)
{
	var action = '\
		<input type="button" value="  Yes  " class="inputs" onclick="jQuery.ggbox.loading(); location.href=\'' + url + '\'"> \
		<input type="button" value="  No  " class="inputs" onclick="jQuery.ggbox.close()"> \
	';

	jQuery.ggbox('Delete Comment', 'Are you sure you want to delete this comment?', action);
	return false;
}

function switchTab(tabSet, numOfTabs, activeTab) {

	$('#tab' + tabSet + current_tab).removeClass("selected");
	$.cookie(tabSet, activeTab, { expires: 30 });
	$('#tab' + tabSet + activeTab).addClass("selected");
	$('#' + tabSet + current_tab).hide();
	$('#' + tabSet + activeTab).show();
	$('#' + tabSet + activeTab).blur();
	current_tab = activeTab;
}

function tabInit(tab, count) {
	var tabval = $.cookie(tab);
	if (null !== tabval) {
		switchTab(tab, count, tabval);
	}
}

function ajaxCall() {
	var args = ajaxCall.arguments;
	switch (args[0]) {
		case "add_to_list":
					jQuery.ggbox.init();
					jQuery.ggbox.loading();
				$.getJSON(webroot + "/ajax.php", {action: args[0], type: args[1], user: args[2]}, function(json){
					var action = '\
						<input type="button" value="  OK  " class="inputs" onclick="jQuery.ggbox.close()"> \
					';
					jQuery.ggbox.reveal(json.title, json.data, action);
				});
		break;
		case "reload_captcha":
			$.getJSON(webroot + "/ajax.php", {action: "reload_captcha"}, function(json){
				$('#captcha_image').attr({ src: webroot + "/image.php?code=" + json.code });
				$('#captcha_code').val(json.code);
			});
		break;
		case "verify_details":
			jQuery.ggbox.init();
			jQuery.ggbox.loading();
			$.getJSON(webroot + "/ajax.php", {action: args[0], user_name: args[1], user_password: args[2], user_password_2: args[3], user_email: args[4], birth_day: args[5], birth_month: args[6], birth_year: args[7], user_sex: args[8], img_pass: args[9], code: args[10]}, function(json){
				jQuery.ggbox.close();

				if (json.valid == 0)
				{
					$('#error').show();
					$('#error').empty();
					$('#error').append(json.error);
				}
				else
				{
					$('#error').hide();
					document.forms['sign_up'].submit();
				}
			});
		break;
	}
}