var FONT_SIZE_COOKIE = 'sustainability_at_sobeys_font_size';
var DEFAULT_TEXT_SIZE = "100";
var LARGE_TEXT_SIZE = "130";

var options = { path: '/', expires: 10 };
var fontSize;

TS = {};
TS.readcookie = {
	init: function() {
		if ( $.cookie( FONT_SIZE_COOKIE ) != null ) {
			fontSize = String( $.cookie( FONT_SIZE_COOKIE ) );
			adjustTextSize( );
		} else {
			fontSize = DEFAULT_TEXT_SIZE;
		}
	}
};

function toggleTextSize( ) {
	if ( fontSize == DEFAULT_TEXT_SIZE ) {
		fontSize = LARGE_TEXT_SIZE;
	} else {
		fontSize = DEFAULT_TEXT_SIZE;
	}
	adjustTextSize();
	$.cookie(FONT_SIZE_COOKIE, fontSize, options);
}

function adjustTextSize() {
	if ( fontSize == DEFAULT_TEXT_SIZE ) {
		//English
		$("#search").find("label").css( { paddingTop: '4px' } );
		$("#search").css( { width: '220px' } );
		$("#search").find("label").css( { width: '35px' } );
		$("#icons").css( { width: '417px' } );
		$("#icons_home").css( { width: '548px' } );
		
		//French
		$("#search_fr").find("label").css( { paddingTop: '4px' } );
		$("#search_fr").css( { width: '240px' } );
		$("#search_fr").find("label").css( { width: '57px' } );
		$("#icons_fr").css( { width: '395px' } );		
		$("#icons_home_fr").css( { width: '528px' } );
		
		//Both
		$("#download").css( { width: '120px' } );
	}else{
		//English
		$("#search").find("label").css( { paddingTop: '3px' } )
		$("#search").css( { width: '231px' } );
		$("#search").find("label").css( { width: '48px' } );
		$("#icons").css( { width: '404px' } );
		$("#icons_home").css( { width: '535px' } );
				
		//French
		$("#search_fr").find("label").css( { paddingTop: '3px' } )
		$("#search_fr").css( { width: '259px' } );
		$("#search_fr").find("label").css( { width: '77px' } );
		$("#icons_fr").css( { width: '375px' } );
		$("#icons_home_fr").css( { width: '508px' } );

		//Both
		$("#download").css( { width: '160px' } );
	}
	document.body.style.fontSize = fontSize + "%";
}


PEPS = {};
PEPS.rollover =
{
 init: function()
 {
	this.preload();
	
	$("#top_nav").find("img:not(.current)").hover(
	   function () { $(this).attr( 'src', PEPS.rollover.newimage($(this).attr('src')) ); },
	   function () { $(this).attr( 'src', PEPS.rollover.oldimage($(this).attr('src')) ); }
	);
 },
 
 preload: function()
 {
	$(window).bind('load', function() {
	   $("#top_nav").find("img:not(.current)").each( function( key, elm ) { $('<img>').attr( 'src', PEPS.rollover.newimage( $(this).attr('src') ) ); });
	});
 },
 
 newimage: function( src ) { return src.substring( 0, src.search(/(\.[a-z]+)/) ) + '-Current' + src.match(/(\.[a-z]+)/)[0]; },
 oldimage: function( src ) { return src.replace(/-Current/, ''); }
};

///////////////////////

function popUp(URL,w,h) {
	popupScroll(URL,w,h,"1")
}


function popupNoScroll(URL,w,h){
	popupScroll(URL,w,h,"0")
}

function popupScroll(URL,w,h,scroller){
	var newwin = window.open(URL, 'sobpop', 'toolbar=0,scrollbars='+scroller+',location=0,statusbar=0,menubar=1,resizable=1,width='+w+',height='+h+',left = 420,top = 150');
	newwin.focus();
}

function emailAFriend(){
   var submitUrl = "/sustainability/email_post.aspx";
   var obj = this;
   var femail = document.getElementById('from_email').value;
   var temail = document.getElementById('to_email').value;
   var url = top.location.href;
   $.post(submitUrl, {fromEmail:femail,toEmail:temail,url:url}, function(response) {obj.showEmailAFriencResponse(response);});
}

function showEmailAFriencResponse(callBackMsg){
	alert("Your message has been sent.")
	TINY.box.hide();
}

function emailAFriendFrench(){
   //var submitUrl = "/ladurabilite/email_post.aspx";
   var submitUrl = "/sustainability/email_post_fr.aspx";
   var obj = this;
   var femail = document.getElementById('from_email').value;
   var temail = document.getElementById('to_email').value;
   var url = top.location.href;
   $.post(submitUrl, {fromEmail:femail,toEmail:temail,url:url}, function(response) {obj.showEmailAFriencResponseFrench(response);});
}

function showEmailAFriencResponseFrench(callBackMsg){
	alert("Votre courriel a \u00E9t\u00E9 envoy\u00E9.")
	TINY.box.hide();
}

jQuery(function() {
	PEPS.rollover.init();
	TS.readcookie.init();
	
	$("#email").click( function() { 
		TINY.box.show('/sustainability/email/email.html',1,300,240,1);
		return false;
	});
	
	$("#email_fr").click( function() { 
		TINY.box.show('/ladurabilite/email/email.html',1,340,240,1);
		return false;
	});
	
	$("#pdf").click( function() { 
		TINY.box.show('/sustainability/pdf/index.html',1,290,185,1);
		return false;
	});
	
	$("#pdf_fr").click( function() { 
		TINY.box.show('/ladurabilite/pdf/index.html',1,310,185,1);
		return false;
	});
	
	$("#print").click( function() { 
		window.print();
		return false;
	});
	
	$("#font").click( function() { 
		toggleTextSize();
		return false;
	});

});