/*
 * Javascript code for gavekalenderen.no
 * 
 */

$(document).ready(function(){
	// adds date picker to date fields
	$('.datefield').datepicker({
		showOn: 'focus',
		changeMonth: true,
		changeYear: true,
		currentText: 'I dag',
		dateFormat: 'dd.mm.yy',
		dayNames: ['Søndag','Mandag','Tirsdag','Onsdag','Torsdag','Fredag','Lørdag'],
		dayNamesMin: ['Sø','Ma','Ti','On','To','Fr','Lø'],
		dayNamesShort: ['Søn','Man','Tir','Ons','Tor','Fre','Lør'],
		duration: 'fast',
		firstDay: 1,
		gotoCurrent: true,
		monthNames: ['Januar','Februar','Mars','April','Mai','Juni','Juli','August','September','Oktober','November','Desember'],
		monthNamesShort: ['Jan','Feb','Mar','Apr','Mai','Jun','Jul','Aug','Sep','Okt','Nov','Des'],
		nextText: 'Neste',
		prevText: 'Forrige',
		yearRange: '-100:+100',
		defaultDate: '01.01.1990'
	});

	$('.datefield2').datepicker({
		showOn: 'button', 
		buttonImageOnly: true, 
		buttonImage: '/img/calendar.png',
		changeMonth: true,
		changeYear: true,
		currentText: 'I dag',
		dateFormat: 'dd.mm.yy',
		dayNames: ['Søndag','Mandag','Tirsdag','Onsdag','Torsdag','Fredag','Lørdag'],
		dayNamesMin: ['Sø','Ma','Ti','On','To','Fr','Lø'],
		dayNamesShort: ['Søn','Man','Tir','Ons','Tor','Fre','Lør'],
		duration: 'fast',
		firstDay: 1,
		gotoCurrent: true,
		monthNames: ['Januar','Februar','Mars','April','Mai','Juni','Juli','August','September','Oktober','November','Desember'],
		monthNamesShort: ['Jan','Feb','Mar','Apr','Mai','Jun','Jul','Aug','Sep','Okt','Nov','Des'],
		nextText: 'Neste',
		prevText: 'Forrige',
		yearRange: '-100:+100',
		defaultDate: '01.01.1990'
	});

	// add a place for dialogs to live
	$('#wrapper').append('<div id="ui-dialog" style="display:none"><iframe id="ui-iframe" style="width:100%;height:100%;" frameborder="0" ></iframe></div>');

	// add a opener for dialogs
	$('#ui-dialog').dialog({
		bgiframe: true,
		autoOpen: false,
		height: 600,
		width: 700,
		modal: true,
		title: '',
		close: function(event, ui) { 
			$('#wrapper').append('<div id="ui-dialog-spinner" style="position:absolute; top:0; left:0; height:100%;width:100%; background-color:silver; z-index:99;"><img src="/img/ajax-loader.gif" alt="Laster inn..." style="margin-left:50%; margin-top:20%;"/></div>');
			$('#ui-dialog-spinner').fadeTo(0,0.5);
			window.location.href = document.location.href;  
		}

	});
	

	// open prododuct dialog on click on miniicon
	$('.miniicon').click(function(event){
		var action='/widget/dialog'+$(this).attr('id');
		action=action.replace(/_/g,"/")
		$('#ui-iframe').attr('src',action);
		$('#ui-dialog').dialog('open');
		event.preventDefault();
	});

	//open product dialog on click on groupmessage
	$('#groupmessage').click(function(event){
		var action='/widget/dialoggroupmessage/userspecialdayid/'+$('#specialday_add_friend_id').val();
		$('#ui-iframe').attr('src',action);
		$('#ui-dialog').dialog('open');
		event.preventDefault();
	});
	//open product dialog on click on groupgift
	$('#groupgift').click(function(event){
		var action='/widget/dialoggroupgift/userspecialdayid/'+$('#specialday_add_friend_id').val();
		$('#ui-iframe').attr('src',action);
		$('#ui-dialog').dialog('open');
		event.preventDefault();
	});

	//adds a user to a userspecialday
	$('#specialday_add_friend_id_drp').change(function(event){
		if ($('#specialday_add_friend_id').val()){
			$('#specialday_add_friend').submit();
			
		}
	});

	
	$('#dialogreminderbutton').live('click',function(event){
		//alert($('#dialogreminderchechbox').val());
		$('#ui-dialog').load("/widget/dialogreminder/",{remindercheckbox:$('dialogremindercheckbox').val()});
	});
	
	//shows user special day form when the user selects a systemspecialday from dropdown
	$('#systemspecialday_id').change(function(event){
		if ($('#systemspecialday_id').val()){
			window.location.href=$('#clonesystemspecialday').attr('action')+'/systemspecialday/'+$('#systemspecialday_id').val();
			event.preventDefault();
		}
	});



	// handles click on forgot password. Replaces password dialog
	$('.forgottenPassword').live('click',function(event){
		$('#userNav').load('/user/resetpassword',function(){
			resetClearOnFocus();
			$('#reset_cellphone').focus();
			pageTracker._trackEvent('Password', 'Password forgotten');
		});
		event.preventDefault();
	});

	// shows reset password form
    $('.resetButton').live('click',function(event){
    	$('#userNav').load('/user/resetpassword',{cellphone:$('#login .inputText').val()});
    	event.preventDefault();
    });  	
        
	//bind clicks on register button, sends cellphone number to server, and replaces #register with returned HTML. Easier than rebuilding document, and moves handling of templates to server
	$('.registerButton').live('click',function(event){
		$('#register').load('/user/registercellphone',{cellphone:$('#register_cellphone').val()});

		/*alert($('#register').val());
		if ($('#login').val().indexOf("Bekreft") > 0) {
    			$('#userNav').load('/user/empty',{cellphone:$('#login .inputText').val()});
		}*/
		pageTracker._trackEvent('Password', 'Password order');
		event.preventDefault();
	});
	$('.registerconfirmButton').live('click',function(event){
		$('#register').load('/user/confirmcellphone',{password:$('#register_cellphone').val()});
		pageTracker._trackEvent('Password', 'Password confirmation');
		event.preventDefault();
	});	

	//bind clicks on register button, sends cellphone number to server, and replaces #register with returned HTML. Easier than rebuilding document, and moves handling of templates to server
	$('.registerButtonAnonymous').live('click',function(event){
		$('#register2').load('/user/registercellphone/anonymous/true',{cellphone:$('#register_cellphone').val()});
		pageTracker._trackEvent('Password', 'Anonymous password order');
		event.preventDefault();
	});
	$('.registerconfirmButtonAnonymous').live('click',function(event){
		$('#register2').load('/user/confirmcellphone/anonymous/true',{password:$('#register_cellphone').val()});
		pageTracker._trackEvent('Password', 'Anonymous password confirmation');
		event.preventDefault();
	});	

	resetClearOnFocus();
	//clear text on focus, reinstate on blur if empty. Unbind when user add text on her own
	// live does not work on focus, so we must reinitialize this on every dom rewrite
	function resetClearOnFocus(){
		$('.clearText, #FilterTextBox').unbind('focus')
		.unbind('blur')
		.unbind('change')
		.focus(function(event){
			
			$(this).attr('prevvalue',$(this).val());
			$(this).val('');
			if ($(this).hasClass('passText')){ // is a password field currently holding plaintext
				//this.type='password'; // changing type via jQuery crashes FF
			}
		})
		.blur(function(event){
			if ($(this).hasClass('passText')){
				//this.type='text'; // changing type via jQuery crashes FF
			}		
			if ($(this).val() == ''){
				$(this).val($(this).attr('prevvalue'));
			}	
		})
		.change(function(event){
			$(this).unbind('focus');
			$(this).unbind('blur');
		});
	}
	$('#password-clear').focus(function() {
	    $('#password-clear').hide();
	    $('#password-password').show();
	    $('#password-password').focus();
	});
	$('#password-password').blur(function() {
	    if($('#password-password').val() == '') {
	        $('#password-clear').show();
	        $('#password-password').hide();
	    }
	});

 });

function loadPopup() {

	if ($('#overlayfg').is(':visible')) {
	        $('#popupContent').hide();

	        $('#overlayfg').hide();
	} else {
		$('#popupContent').show();

		$('#overlayfg').show();
	}
}

function closePopup() {
        $('#popupContent').hide();

        $('#overlayfg').hide();
}


function loadPicturePopup(id) {
	$('#picturePopupContent_' + id).show();
}

function closePicturePopup(id) {
        $('#picturePopupContent_' + id).hide();
}

