var now = new Date();
document.cookie = "lucidipedia_timezone=" + escape(now.getTimezoneOffset()) + ";path=/"; 

function switch_lucid() {
	lucid1=window.document.save_form.lucid1.checked;
	lucid2=window.document.save_form.lucid2.checked;
	lucid3=window.document.save_form.lucid3.checked;
	if (lucid1) {
		window.document.save_form.purpose.disabled=true;
		window.document.save_form.technique.disabled=true;
		window.document.save_form.technique_time.disabled=true;

		window.document.save_form.purpose.value="";
		window.document.save_form.technique.value="";
		window.document.save_form.technique_time.value="00:00";

	}
	if (lucid3 || lucid2) {
		window.document.save_form.purpose.disabled=false;
		window.document.save_form.technique.disabled=false;
	}
}

function switch_time() {
	technique = window.document.save_form.technique.value;

	if (technique == 'None' || technique == '') {
		window.document.save_form.technique_time.disabled = true;
		window.document.save_form.technique_time.value="00:00";
	}
	else {
		window.document.save_form.technique_time.disabled = false;
	}
}

function switch_public() {
	if ($('#public').attr('checked')) {
		$('#global_public').removeAttr('disabled');
		$('#facebook').removeAttr('disabled');
		$('#twitter').removeAttr('disabled');
	} else {
		$('#global_public').attr('disabled','disabled');
		$('#facebook').attr('disabled','disabled');
		$('#twitter').attr('disabled','disabled');
	}
}

function StartCountDown(myDiv,myTargetDate)
{
	var dthen	= new Date(myTargetDate);
	var dnow	= new Date();
	ddiff		= new Date(dthen-dnow);
	gsecs		= Math.floor(ddiff.valueOf()/1000);
	CountBack(myDiv,gsecs);
}

function Calcage(secs, num1, num2)
{
	s = ((Math.floor(secs/num1))%num2).toString();
	if (s.length < 2)
	{
		s = "0" + s;
	}
	return (s);
}

function CountBack(myDiv, secs)
{
	var out;
	var Days = Calcage(secs,86400,100000);
	var Hours = Calcage(secs,3600,24)
	var Minutes = Calcage(secs,60,60)
	var Seconds = Calcage(secs,1,60)

	if(Days != 0){out = Days +" day"+((Days!=1)?"s":"")+", ";}
	if(Days != 0 || Hours != 0){out += Hours +" hour"+((Hours!=1)?"s":"")+", ";}
	if(Days != 0 || Hours != 0 || Minutes != 0){out += Minutes +" minute"+((Minutes!=1)?"s":"")+", ";}
	out += Seconds +" seconds";

	if(secs > 0)
	{
		document.getElementById(myDiv).innerHTML = out;
		setTimeout("CountBack('" + myDiv + "'," + (secs-1) + ");", 990);
	}
	else
	{
		document.getElementById(myDiv).innerHTML = "Now";
	}
}

function switch_tab (tab) {
	$('.switchbox_on').removeClass('switchbox_on');
	$('#' + tab + '_switchbox').addClass('switchbox_on');
	$("div[name]='tabbox'").hide();
	$('#' + tab + '_box').show();
}

function show_popup (type,page,options) {
	var doc_height = jQuery(document).height();
	var popup_offset = jQuery(window).width() / 2;
	var top_offset = jQuery(window).scrollTop() + 20;
	jQuery('#overlay').height(doc_height);
	jQuery('#overlay').fadeTo(400,0.5);
	jQuery('#popup').css('left', popup_offset);
	jQuery('#popup').css('top', top_offset);
	//jQuery('#popup').height('20');
	jQuery('#popup').show();
	//jQuery('#popup').animate({width: 500, left: '-=250'}, 500, 'easeInOutExpo');
	//jQuery('#popup').animate({height: 405}, 500, 'easeInOutExpo');
	xajax_load_popup(type,page,options);
	//jQuery('#popup').queue(function() {
  //	jQuery(this).dequeue();
	//});
}

function hide_popup () {
	jQuery('#popup_content_box').text('');
	var doc_height = jQuery(document).height();
	var popup_offset = jQuery(window).width() / 2;
	//jQuery('#popup').animate({height: 20}, 500, 'easeInOutExpo');
	//jQuery('#popup').animate({width: 1, left: '+=250'}, 500, 'easeInOutExpo');
	jQuery('#popup').hide();
  jQuery('#overlay').fadeOut(400);
	//jQuery('#popup').queue(function() {
  //	jQuery(this).dequeue();
	//});
}

function load_captcha () {
  Recaptcha.create("6LfUULoSAAAAAJsd5EcwaZGCzB-5G-On7aW6EkTo",
		"recaptcha_image", {
   		theme: "custom"
	});
}

function show_feedback (feedback_type) {
	jQuery('#feedback_box').addClass('feedbackbox_'+feedback_type);
	jQuery('#feedback_box').fadeIn(500);
	//$('#feedback_box').effect('shake', { times:1 }, 'fast');
	//$('#feedback_box').delay('10000');
	//$('#feedback_box').fadeOut(2000);
}

function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

function lp_check_cookie(redirect) {
	if (!Get_Cookie('lucidipedia')) {
		show_feedback('bad');
	} else if (redirect) {
		document.location="/profile/update/";
	}
		
}
