/********************************************************************************
| Custom Javascript
| Created for Clearnet.com
| Created by WebsiteCM Software Inc.
*********************************************************************************
| Custom javascript common to all customer pages
********************************************************************************/

// Rollover Images
$(function() {
    $('img[data-hover]').hover(function() {
        $(this).attr('tmp', $(this).attr('src')).attr('src', $(this).attr('data-hover')).attr('data-hover', $(this).attr('tmp')).removeAttr('tmp');
    }).each(function() {
        $('<img />').attr('src', $(this).attr('data-hover'));
    });
});

// Coverage image clicks
$(function() {
    $('img[data-click]').click(function() {
		// Check if this button is already the selected province								
		if ($(this).attr('id') != "current-prov-selected"){		
			// Reset the button graphic for the selected one if one is selected(and it exists)
			if ($('#current-prov-selected').length > 0){
				var originalsrc = $("#current-prov-selected").attr('name');
				originalsrc = originalsrc.replace('coverage-nav-', '');
				originalsrc = "/skin/common_files/images/custom/coverage/"+originalsrc+".png";		
				$("#current-prov-selected").attr("src", originalsrc);
			}
			// Reset the current selected id for all the buttons
			$(".coverage-prov-button").attr('id',"");		
			// Set this current button as the selected
			$(this).attr('id',"current-prov-selected");	
			var neworiginalsrc = $(this).attr('name');
			neworiginalsrc = neworiginalsrc.replace('coverage-nav-', '');		
			
			// Set the header text to change when selection changes
			var newselectedprov = neworiginalsrc;
			if ($("#coverage-header-text-"+newselectedprov).length)
			{
			// Set the header div id for a specific province
			var headertxt = $('#coverage-header-text-'+newselectedprov).html();
			$(".category-descr").show();	
			$("#coverage-footnote-one").show();			
			}
			else
			{
			// Set the header div id for the rest of canada
			var headertxt = $('#coverage-header-text-canada').html();		
			$(".category-descr").hide();
			$("#coverage-footnote-one").hide();			
			}
			
			$('#coverageheader').html(headertxt);
			//
			
			neworiginalsrc = "/skin/common_files/images/custom/coverage/"+neworiginalsrc+"-selected.png";				
			$("#current-prov-selected").attr("src", neworiginalsrc);
			// set the background to show that its selected
	
		}
		else
		{
			// This province is already selected, dont do anything
		}
		// Set the Big Map Image source to be the data-click attribute from the province buttons
		$("#map-image").attr('src', $(this).attr('data-click'));
		$("#map-image").attr("src",$(this).attr('data-click')).stop(true,true).hide().fadeIn();
		
    });
});

// Coverage buttons change on hover
$(function() {		   
  $(".coverage-prov-button").hover(function() {			
	// If the current button we are hovering isnt the selected one we change the image source when the mouse enters 												
	if ($(this).attr('id') != "current-prov-selected"){													
    	$(this).attr("src", $(this).attr("src").split(".").join("-hover."));
	}		
  }, function() {
	// If the current button we are hovering isnt the selected one we change the image source back when the mouse leaves	  
	$(this).attr("src", $(this).attr("src").split("-hover.").join("."));
  });
});

// Postal Code Checker Popup
$(document).ready(function() {						   
	$(".postalcode").colorbox( {
		iframe:true, innerWidth:320, innerHeight:290, scrolling:false, opacity:.6, overlayClose:false
	}
	);
});

// Nav Action Box (the green box under the navigation)
$(document).ready(function() {	
	if ($('.nav-action-box').length){
		setTimeout(function(){
			$('.nav-action-box').effect("bounce", { times:5, direction:'down' }, 300);
		}, 1000);
		setTimeout(function(){
			if(jQuery.browser.msie)		
				$('.nav-action-box').delay(1500).hide();		
			else	
				$('.nav-action-box').delay(1500).fadeOut();
		}, 3000);
		
		
	}					   
});

// Minicart flash item
var pause = false;
// Flashes left before we stop
var rf = 3;
function blink(rf) 
{
  if (rf > 0)	{
	  $("#need-item").fadeIn(600,function(){
		if (pause) return;
		$("#need-item").fadeOut(2000, function() {				     
		  rf = parseInt(rf) - 1;									   
		  blink(rf);
		  return rf;
		});  
	  });
  }
  else
  {
	  $("#need-item").fadeIn(600);	  
  } 
  
}
$(document).ready(function () 
{
	rf = blink(rf);
	$("#need-item").mouseover(function(){
		pause=true;
    });
	$("#need-item").mouseout(function(){
		pause=false;
		rf = blink(rf);
    });
});

// Calendar Date Picker on birthdate in checkout
$(function() {
	$("#birthday" ).datepicker( {
		showOn: "button",
		buttonImage: "/skin/common_files/images/custom/calendarpicker.gif",
		buttonImageOnly: true,
		changeYear: true,
		minDate: '-110y',
		maxDate: '-18y',
		yearRange: '-110:+0',
		showAnim: 'slideDown'
	}
	);
}
);

// Calendar Date Picker
$(function() {
	$("#license_expiration" ).datepicker( {
		showOn: "button",
		buttonImage: "/skin/common_files/images/custom/calendarpicker.gif",
		buttonImageOnly: true,
		changeYear: true,
		yearRange: '-10:+10',
		showAnim: 'slideDown'
	}
	);
}
);

// Slide the phones box across when the Details button is clicked
function phoneslide(divid, direction)
{
	if (direction == "left"){
	$('#'+divid).animate({
    left: '-320'
	  }, 1000, function() {
		// Animation complete.
	  });		
	}
	else if (direction == "right"){
	$('#'+divid).animate({
    left: '0'
	  }, 1000, function() {
		// Animation complete.
	  });		
	}
	
}

// Show / Hide Divs
function showhide(divid, state)
{
	document.getElementById(divid).style.display=state
}

// Data notification sign-up
$(document).ready(function() {
	$("#data-signup").click(function() {
    	var checked_status = this.checked;
        if (checked_status == true) {
			showhide('data-signup-originalstate','none');
			showhide('data-signup-formstate','block');
		}
	});
});
$(document).ready(function()
{
    $('#data-signup-submit').click(function ()
	{
		dataSignUpSubmit();
    });
}); 

$(document).ready(function()
{
	$("#email").keydown(function (event) {
		if (event.keyCode == 13 || event.keyCode == 108)
		{
			event.preventDefault();
			dataSignUpSubmit();
		}
	});
});

function dataSignUpSubmit()
{
	var email = $('input[name=email]');
	var postal_code = $('input[name=postal_code]');
	
	if (email.val()=='') {
		return false;
	}
	 
	var data = 'email=' + email.val() + '&postal_code=' + postal_code.val();
	
	// Send to php
	$.ajax({
		url: "/data_signup.php", 
		type: "GET",
		data: data,     
		cache: false,
		success: function (html)
		{
			if (html==1) {                  
				$('.form').fadeOut(0);                 
				$('.done').fadeIn('slow');
			}
			else
			{
				xAlert('Please specify a valid email address.', 'Data Notification');
			}
		}       
	});
	return false;
}

// Welcome page image sliding
$(document).ready(function() {						   
	$('.welcomefullslide').hover(function(){
		$(this).stop().animate({top:'-260px'},{queue:false,duration:200});
	}, function() {
		$(this).stop().animate({top:'0px'},{queue:false,duration:200});
	});	
	$('.welcomesmallslide').hover(function(){
		$(this).stop().animate({top:'-130px'},{queue:false,duration:200});
	}, function() {
		$(this).stop().animate({top:'0px'},{queue:false,duration:200});
	});	

}
);

// When the province selection mouseover happens swap the image src
$(document).ready(function () 
{
	$('#map-area-bc').hover(
		function() {
		$("#ab-selection").hide();			
		$("#no-selection").stop().animate({"opacity": "0"}, "slow");
		},
		function() {
		$("#no-selection").stop().animate({"opacity": "1"}, "slow");
		$("#ab-selection").show();			
		});
		 
	$('#map-area-ab').hover(
		function() {
		$("#bc-selection").hide();
		$("#no-selection").stop().animate({"opacity": "0"}, "slow");
		},
		function() {
		$("#no-selection").stop().animate({"opacity": "1"}, "slow");
		$("#bc-selection").show();		
		});
});
