/*

	Author: Suciu Vlad
	
*/

$(document).ready(function(){
	
	var current;
	
	$('#frmLogin, #frmSubscribe, #frmSign, #frmSearch').find('input').focus(function(){
		current = $(this).val();
		$(this).val('');
	})
	
	$('#frmLogin, #frmSubscribe, #frmSign, #frmSearch').find('input').blur(function(){
		if($(this).val() == '')
			$(this).val(current);
	})
	
	if($('#userAlert').length){
		
		$('#userAlert').css('display', 'none');
		$('#userAlert').slideToggle();
		
		setTimeout(function(){
			$('#userAlert').slideToggle();
		}, 2500)
		
	}
	
	$('.styledRadio input').css('display', 'none');
	
	$('.styledRadio dl dt a').click(function(){
		
		$(this).parent().parent().find('a').removeClass('on');
		$(this).addClass('on');
		$(this).next().trigger('click');
		
		return false;
	})
	
	var server_minutes = $('#ajax_timer_minutes').val();
	var server_seconds = $('#ajax_timer_seconds').val();
	
	setInterval(function(){
	
		if(server_seconds <= 0){
			server_seconds = 59;
			server_minutes -= 1;
		}else{
			server_seconds -=1 ;
		}
		
		if(server_minutes <= 0 && server_seconds <= 0)
				window.location = "expired";
		
		$('#ajax_timer_seconds').parents().find('.info').find('.sec').html(server_seconds.toFixed(0));
		$('#ajax_timer_minutes').parents().find('.info').find('.min').html(Math.ceil(server_minutes));
			
	}, 1000) //refresh the timer every second
		
	
	//quiz content hide
	
	if($('.hookSmallBox').length > 1){
		$('.hookSmallBox:not(:last)').hide();
		$('.hookSmallBox:not(:last)').parent().find('.switch').addClass('switchOn');
	}
	
	$('.hookSmallBox').parent().find('.switch').click(function(){
		var parent = $(this).parent().find('.hookSmallBox');
		
		parent.slideToggle("fast");
		$(this).toggleClass('switchOn');
		
		return false;
	})
	
	$('#frmGmail').live('submit', function(){
		
		var elem = $(this), 
			location = $(this).attr('action');

		submit_gmail(elem, location);
		
		return false;
	})
	
	/* --------- */
	
	function verify_credentials(elem, location){
		
		var data = elem.serialize();
		$.ajax({
			type: "POST",
			url: location,
			data: data,
			dataType: "json",
			beforeSend: function(){
				$('#ajaxResponse').html('<div class="img" />');
			},
			success: function(result){
				
				if(result.result == "success"){
					$('#ajaxResponse').html(result.message);
					$('#frmEmail').hide();
				}else{
					$('#ajaxResponse').html('');
					user_alert(result.message);
				}
			}
		})
		
	}
	
	
	$('#frmEmail').live('submit', function(){
		
		var elem = $(this), 
			location = $(this).attr('action');

		verify_credentials(elem, location);
		
		return false;
	})
	
	function user_alert(message){
		
		if($('#userAlert').length)
			$('#userAlert').remove();
		
		$('body').prepend(
							"<div id='userAlert'>"
							+message
							+"</div>"
						);
						
		$('#userAlert').hide().slideToggle("fast");
		
		setTimeout(function(){
			$('#userAlert').slideToggle();
		}, 2500)
			
	}
	
	
	
	/* START HOTMAIL SCRIPT */
	
	
	
	$('div.page.refer div.select a#hotmail').click(function(){
		
			var url = window.location.href.toString(),
			returnURI = url + "/live_delegation",
			privacy = url + "/privacy",
			PermissionRequestURI = "https://consent.live.com/Delegation.aspx?RU=" + (returnURI) + "&ps=" + "Contacts.View" + "&pl=" + (privacy);
			
			window.open(PermissionRequestURI, "", "width=760px,height=560,left=100,top=100,scrollbars=yes, location=yes");
			
			$('div.page.refer div.select ul').hide();
			$('div.page.refer #frmEmail').hide();
			
			return false;
		})
		
		$('div.page.refer div.select a#gmail').click(function(){
		
			$('#frmEmail').show();
			
			$('#ajaxResponse').html('');
			
			$('div.page.refer div.select ul').hide();
	
			return false;
	})
	
	
	
	/* END HOTMAIL SCRIPT */

	$('div.page.refer div.select > a').click(function(){
	
		var el = $(this).parent().find('ul').toggle();
	
		return false;
	})
	
	$('#frmContacts').find('a.refer').click(function(){
		
		var el1 = '<li class="first">';
				el1 += '<label for="name">Friend Name: </label>';
				el1 += '<span class="inputRight">';
				el1 += '<span class="inputLeft">';
				el1 += '<input type="text" name="name[]" id="name">';
				el1 += '</span>';
				el1 += '</span>';
				el1 += '</li>';
				
		var el2 = '<li class="clone">';
				el2 += '<label for="name">Friend Email: </label>';
				el2 += '<span class="inputRight">';
				el2 += '<span class="inputLeft">';
				el2 += '<input type="text" name="email[]" id="email">';
				el2 += '</span>';
				el2 += '</span>';
				el2 += '</li>';
			
			$(this).parent().parent().append(el1).append(el2);
		
		return false;
	})
	
	$('#ajaxResponse span.check').find('.checkAll').live('click', function(){
		
		$('#ajaxResponse').find('input[type=checkbox]').attr("checked", true);
		
		return false;
	})
	
	$('#ajaxResponse span.check').find('.checkNone').live('click', function(){
		
		$('#ajaxResponse').find('input[type=checkbox]').attr("checked", false);
		
		return false;
	})
	
	//zebra rows
	$('table.listings tbody tr:even').css('backgroundColor', '#f1f3fb');
	$('table.listings tbody tr:odd').css('backgroundColor', '#ffffff');
	//quiz result listings
	$('table.listings td.title').find('a').click(function(){
		
		var row = $(this).parent().parent();
		var rowColor = row.css('backgroundColor');
		
		var target = row.find('div.listingDetails');
		row.css('backgroundColor', '#e0edff');
		
		if(target.css('display') == 'block'){
			target.hide();
			$('table.listings tbody tr:even').css('backgroundColor', '#f1f3fb');
			$('table.listings tbody tr:odd').css('backgroundColor', '#ffffff');
		}else{
			$('table.listings').find('.listingDetails').hide();	
			target.fadeIn('fast');
		}
		
		
		return false;
	})
	
	
	//quiz results
	$('table.listings input').css('display', 'none');
	$('table.listings a.platform').click(function(){
		
		var row = $(this).parent().parent().parent().parent().parent().parent();
		
		//trigger input
		row.find('input[type=radio]').trigger('click');
		var value = row.find('input[type=radio]').attr("value");
		
		//show selected, hide others
		$('table.listings td.last img').css('display', 'none');
		row.find('td.last img').css('display', 'block');
		
		//close block
		row.find('td.title a').trigger('click');
		
		//ajaxify the download instructions
		
		$.ajax({
			type: "POST",
			url: $('#path').attr('value'),
			data: 'id_room='+value,
			success: function(result){
				$('#ajax_instructions').html(result);
				$('h4#data span').html('3');
			}
		})
		
		return false;
	})
	
	//check radio
	$('table.listings tr input[type=radio]:checked').parent().find('img').show();
	
	//phone verification
	$('div a.verifyPhone').click(function(){
		
		//window popup
		var url = $(this).attr('href');
		window.open(url, "", "width=760px,height=560,left=100,top=100,scrollbars=yes");
		
		
		return false;
	})
	
	
	
})
