/* Site Framework */

// Global Variables
var site = "http://www.rexall.ca/"
var images = site + "assets/images/";
var styles = site + "assets/styles/";
var ajax = "http://www.rexall.ca/assets/ajax/";
var scripts = site + "assets/scripts/";
var podcasts = site + "assets/podcasts/";
var storefiles = site + 'assets/storefiles/';
var healthadvisor = site + "healthadvisor/";
var mediacentre = site + "mediacentre/";
var storelocator = site + "storelocator/";
var lightwindows = site + "lightwindows/";
var lightwindow = null;
var healthandwellness = healthadvisor + "healthandwellness/";
var conditions = healthadvisor + "conditions/";
var naturalproducts = healthadvisor + "naturalproducts/";
var medications = healthadvisor + "medications/";
var instore = site + "instore/";
var advantage = site + "advantage/";

// Document Ready (On Page Load)
jQuery(document).ready(function(){
	// Initialize Font Sizing & Selection
	jQuery("body").css('font-size', jQuery("#styleSelect a.selected").attr('id').substring(4,6) + 'px');

	// Navigation Menu
	jQuery("#navigation li.navLink").hover(function(){ jQuery(this).find('ul').show(); jQuery(this).find('img').attr('src', jQuery(this).find('img').attr('src').replace('_off', '_over')); }, function(){ jQuery(this).find('ul').hide(); jQuery(this).find('img').attr('src', jQuery(this).find('img').attr('src').replace('_over', '_off'));});
	jQuery("#navigation li ul li:last-child").addClass('bottom');
	
	// Initialize Lightwindows
	jQuery('#dialog').jqm();

	// Fix PNG Images
	jQuery("img:not(.dialog)").ifixpng();
	
	// Signup Button Fly Out Tab						 
   jQuery("#signUpButton").hover( function(){ jQuery("#signUpTab").show().animate({width: "232px"}, "slow" ); }, function(){ setTimeout(function(){ jQuery("#signUpTab").animate({width: "0px"}, "slow", function(){jQuery("#signUpTab").hide();} ); }, 3000); } );  
	
	// Initialize Google Analytics
	googleAnalytics();

	// Search
	jQuery("#searchTerm").focus(function(){ if (jQuery(this).val() == 'Health Advisor Search') jQuery(this).val(''); }).blur(function(){ if (jQuery(this).val() == '') jQuery(this).val('Health Advisor Search'); });

	// Site Search
	jQuery("#rexallSearch").submit(function(){
		if (jQuery("#rexallSearchTerm").val() == '')
		{
			alert('Please enter a search term');
			return false;
		}
		else if (jQuery("#rexallSearchTerm").val().length < 3)
		{
			alert('Your search term must be at least 3 characters in length');
			return false;
		}
	});
	
	// DIN Info Box
	if(!document.getElementById('DINInfo'))
	{
	   jQuery("body").append('<img id="DINInfo" src="' + images + 'DIN.jpg" />');
	   jQuery("#DINInfo").css({position: "absolute", "z-index": "1001", "display": "none"});
	}	
	jQuery("#searchText img").mousemove(function(e)
	{
		var element = jQuery(this);			
		jQuery("#DINInfo").css({"left": e.pageX - 100, "top" : e.pageY - 250}).fadeIn('fast');
	}).mouseout(function()
	{ jQuery("#DINInfo").hide().css({"left": 0, "top" : 0}); });

	// Preload Imagery
	preload('navigation_healthadvisor_over.gif');
	preload('navigation_pharmacy_over.gif');
	preload('navigation_instore_over.gif');
	
});

jQuery(function(){
	// Lightwindows
	jQuery(".newsletterBeautyEventsSignup").click(function(){ openInFrame(lightwindows + 'newsletter?beautyEvents=1'); });
	jQuery(".newsletterSignup").click(function(){ openInFrame(lightwindows + 'newsletter'); });
	jQuery(".airmiles").click(function(){ openInFrame("http://rexall.ca/global/airMiles.aspx"); });
	jQuery(".contact").click(function(){ openInFrame(lightwindows + 'contact'); });
	jQuery(".help").click(function(){ openInFrame(lightwindows + 'help'); });
	jQuery(".legal").click(function(){ openInFrame(lightwindows + 'terms'); });
	jQuery(".consent").click(function(){ openInFrame(lightwindows + 'consent'); });
	jQuery(".privacy").click(function(){ openInFrame(lightwindows + 'privacy'); });
	jQuery(".thankyouairmiles").click(function(){ openInFrame(lightwindows + 'thankyouairmiles'); });
	jQuery(".thankyouRogersCup").click(function(){ openInFrame(lightwindows + 'thankyouRogersCup'); });
	jQuery(".guidelines").click(function(){ openInFrame(lightwindows + 'guidelines'); });
	jQuery(".askPharmacist").click(function(){ openInFrame(lightwindows + 'healthysuggestions'); });
	jQuery(".heartassessment").click(function(){ openInFrame(lightwindows + 'heartassessment'); });	
	jQuery(".flureadykit").click(function(){ openInFrame(lightwindows + 'flureadykit'); });	

	// Font Sizing
	jQuery("#size12, #size14, #size16").click(function(){ fontSize(jQuery(this).attr('id').substring(4, 6)); });
});

// Forced Lightwindows
function sharewithafriend(title, pagelink) { jQuery.post(ajax + 'sessions.php', { 'session':'socialShare', 'url':pagelink, 'title':title}, function(data) { 	jQuery("#dialog").jqmShow(); openInFrame(lightwindows + 'socialshare'); }); }
function print_mcc(list) { jQuery.post(ajax + 'sessions.php', { 'session':'print_mcc', 'list':list}, function(data) { 	jQuery("#dialog").jqmShow(); openInFrame(lightwindows + 'print_mcc'); }); }
function unsubscribe() { openInFrame(lightwindows + 'unsubscribe'); jQuery('#dialog').jqmShow(); }
function thankyouairmiles() { openInFrame(lightwindows + 'thankyouairmiles'); jQuery('#dialog').jqmShow(); }
function thankyouRogersCup() { openInFrame(lightwindows + 'thankyouRogersCup'); jQuery('#dialog').jqmShow(); }
function newsletter() { openInFrame(lightwindows + 'newsletter'); jQuery('#dialog').jqmShow(); }
function enewsletter() { openInFrame(lightwindows + 'enewsletter'); jQuery('#dialog').jqmShow(); }
function privacy() { openInFrame(lightwindows + 'privacy'); jQuery('#dialog').jqmShow(); }
function contact() { openInFrame(lightwindows + 'contact'); jQuery('#dialog').jqmShow(); }
function consent() { openInFrame(lightwindows + 'consent'); jQuery('#dialog').jqmShow(); }
function terms() { openInFrame(lightwindows + 'terms'); jQuery('#dialog').jqmShow(); }
function help() { openInFrame(lightwindows + 'help'); jQuery('#dialog').jqmShow(); }
function newsletterSignup() { openInFrame(lightwindows + 'newsletter'); jQuery('#dialog').jqmShow(); }
function newsletterBeautyEventsSignup() { openInFrame(lightwindows + 'newsletter?beautyEvents=1'); jQuery('#dialog').jqmShow(); }

// Open Lightwindow IFrame (External URLs)
function openInFrame(url)
{
	jQuery("#lwContent").replaceWith('<iframe id="lwContent" src="" frameborder="0"></iframe>')
	jQuery("#lwContent").html('').attr('src', url);
	jQuery("#dialog img").ifixpng();
}

// Google Analytics
function googleAnalytics()
{
	jQuery.getScript(("https:" == document.location.protocol ? "https://ssl." : "http://www.") + 'google-analytics.com/ga.js', function(){
		// Live Code
		var pageTracker = _gat._getTracker("UA-3955111-1");
		pageTracker._initData();
		pageTracker._trackPageview();
	});
}

// Font Size Adjustment
function fontSize(size)
{
	// Adjust text size and underline new current size
	jQuery("body").css('font-size',size + 'px');
	jQuery("#size" + size).css('text-decoration','underline').siblings().css('text-decoration','none');
	
	// Save Data to Cookie
	jQuery.post(ajax + 'sessions.php', { 'session':'fontsize', 'fontSize':size });
	
}


function preload(preimage)
{
	image = new Image(500,500); 
	image.src = images + preimage;
}



// Pagination
function Pagination(offset, numOfPage, currentPage)
{
	var pageStart = parseFloat(currentPage) - parseFloat(offset);
	var pageEnd = parseFloat(currentPage) + parseFloat(offset);
	var numPage = new String();
	
	if(numOfPage < 1)
		return false;
		
	numPage += '<ul class="pagi">';
	
	
	if(currentPage > 1)
		numPage += '<li class="previous"><a class="click" name="page'+(parseFloat(currentPage) - 1)+'">PREV</a></li>';
	else
		numPage += '<li class="previous-off">PREV</li>';
		
	if(currentPage > (offset + 1))
		numPage += '<li><a class="click" name="page1">1</a></li><li class="spacing-dot"> ... </li>';
		
	for(i = 1; i <= numOfPage; i++)
	{
		if(pageStart <= i && pageEnd >= i)
		{
			if (i == currentPage)
				numPage += '<li class="active">'+i+'</li>';
			else
				numPage += '<li><a class="click" name="page'+i+'">'+i+'</a></li>';
		}
	}
	
	if (numOfPage > pageEnd)
		numPage += '<li class="spacing-dot"> ... </li><li><a class="click" name="page'+numOfPage+'">'+numOfPage+'</a></li>';
		
	if (currentPage < numOfPage)
		numPage += '<li class="next"><a class="click" name="page'+(parseFloat(currentPage) + 1)+'">NEXT</a></li>';
	else
		numPage += '<li class="next-off">NEXT</li>';
		
	numPage += '</ul>';
	
	return numPage;
}
