/* ...................................................

	Ambulance Victoria Javascript functions.

................................................... */

$(document).ready(function(){
									
									
	// check if it's not a landing or homepage.
	// if not, then add text resize control:
	var bodyClass = $('body').attr('class');
	if( !bodyClass.match('home') && !bodyClass.match('landing') ){
		var resizeControlsString = '<ul class="resizeControls">';
		resizeControlsString += '<li class="textUp">Text up</li>';
		resizeControlsString += '<li class="textDown">Text down</li>';
		resizeControlsString += '</ul>';
		$('#subNav').after(resizeControlsString);
		
		// usage of resizeControls:
		$('.resizeControls li.textUp').hover(function(){
			$(this).css({'background-position':'0 -11px'});
		}, function(){
			$(this).css({'background-position':'0 0'});
		});
		$('.resizeControls li.textDown').hover(function(){
			$(this).css({'background-position':'-16px -11px'});
		}, function(){
			$(this).css({'background-position':'-16px 0'});
		});
		
		// check cookie on font size upon page load
		// default setting on font-size:

		var levelone = '1em';
		var leveltwo = '1.16em';
		var levelthree = '1.33em'
		
		var presetFontsize = readCookie('fontsize');
		if(presetFontsize == 1){
			$('#contentWrapInner').css({'font-size':leveltwo});
		}
		else if(presetFontsize == 2){
			$('#contentWrapInner').css({'font-size':levelthree});
		}
		else{
			$('#contentWrapInner').css({'font-size':levelone});
		}
	}
		
	// activate resizeControls
	$('.resizeControls li.textUp').click(function(){
		var level = readCookie('fontsize');
		if(level == 1 || level == 2){
			$('#contentWrapInner').css({'font-size': levelthree});
			createCookie('fontsize', 2, 7);
		}
		else{
			$('#contentWrapInner').css({'font-size': leveltwo});
			createCookie('fontsize', 1, 7);
		}
	});
	$('.resizeControls li.textDown').click(function(){
		var level = readCookie('fontsize');
		if(level == 1 || level == 0){
			$('#contentWrapInner').css({'font-size': levelone});
			createCookie('fontsize', 0, 7);
		}
		else{
			$('#contentWrapInner').css({'font-size': leveltwo});
			createCookie('fontsize', 1, 7);
		}
	});
	
//	// insert print page button:
//	// if there is a content control (content pages)
//	if($('.contentControls').length > 0){
//		//find share button, and add before it:
//		$('li.share', this).before('<li class="print"><a href="javascript:print();">Print Page</a></li>');
//		
//		// activate hover control for share link in content controls
//		var shareLinks = '<ul>';
//		
//		var emailSubject =  "Check out this page on Ambulance Victoria";
//		
//		//%0D%0A - new line ASCII character
//		var emailBody = "Hi,%0D%0A%0D%0AI thought you might be interested in this page I came across on the Ambulance Victoria website:%0D%0A";
//		emailBody += encodeURIComponent(location.href);
//				
//		
//		var emailFriendHref = "mailto:?subject=" + emailSubject + "&body=" + emailBody;
//		
//		shareLinks += '<li class="em"><a href=\'' + emailFriendHref + '\'>Email to a friend</a></li>';	
//	    shareLinks += '<li class="fb"><a href=\'\' onclick="return facebook_share()">Facebook</a></li>';
//		shareLinks += '<li class="digg"><a href=\'\' onclick="return digg_share()">Digg</a></li>';
//		shareLinks += '<li class="del"><a href=\'\' onclick="return delicious_share()">Delicious</a></li>';
//		shareLinks += '<li class="st"><a href=\'\' onclick="return stumbleupon_share()">Stumbleupon</a></li>';
//		$('li.share').append(shareLinks);
//		$('li.share').hover(function(){
//			$('ul', this).show();
//			$(this).addClass('shareover');
//		}, function(){
//			$('ul', this).hide();
//			$(this).removeClass('shareover');
//		});
//	}

	
	
	// declare the select objects that will have custom select
	var customSelectObjects = '#landingActions .form select';
	customSelectObjects += ', #corpLandingActions .form select';
	customSelectObjects += ', #actionPortal select';
	customSelectObjects += ', .moduleForm .formRow select';
	customSelectObjects += ', .moduleHeader .controls select';
	
	// check if they exist on the page, if so, call the plugin
	if($(customSelectObjects).length > 0){
		$(customSelectObjects).selectbox({dropdownBehaviour: 'click'}, {replaceInvisible: true});
	}
	
	// for gallery listing pages
	if($('.galleryListing').length > 0){
		
		// set thumbnails caption to have transparenty bg
		$('.item .caption', this).each(function(){
			//$(this).css({'opacity': 0.2});
		});
		
		// set transparency hover on thumbnails
		$('li.item a', this).hover(function(){
			$('img', this).css({'opacity': 0.8});
		}, function(){
			$('img', this).css({'opacity': 1});
		});
	}
	
	// setting horizontal alignment on:
	var maxHeight = 0;
	// 1. Join Singles page
	
	
	// landing pages - red bit.
	$('.landing #subContent .col').each(function(){
		// get highest col...
		var currHeight = $(this).outerHeight();
		if(currHeight > maxHeight){
			maxHeight = currHeight;
		}
	});
	$('.landing #subContent .col').each(function(){
		// loop again and set all to be maxHeight;
		$(this).height(maxHeight);
		
		// then set all buttons to be absolute position;
		$(this).css({'position':'relative'});
		$('.button', this).css({'position':'absolute', 'bottom':'0', 'left':'15px'});
	});
	
	// landing pages - white bit on corporate landing.
	$('.landing #corpLandingActions .col').each(function(){
		// get highest col...
		var currHeight = $(this).outerHeight();
		if(currHeight > maxHeight){
			maxHeight = currHeight;
		}
	});
	$('.landing #corpLandingActions .col').each(function(){
		// loop again and set all to be maxHeight;
		$(this).height(maxHeight);
		
		// then set all buttons to be absolute position;
		$(this).css({'position':'relative'});
		$('.button', this).css({'position':'absolute', 'bottom':'0', 'left':'15px'});
		// reset corpLanding form button to shift back.
		$('#corpLandingActions .form .button').css('left', 0);
	});
	
	// landing pages - the white bit
	// since first two cols set the height standard, dynamic height will be set on the others.
	// 1. get height of either landingFour or landingFive...
	var landingActionsHeight = $('.landing #landingActions .landingFour').outerHeight();
	// loop through all col and compare height. If less, set height.
	$('.landing #landingActions .col').each(function(){
		if($(this).outerHeight() < landingActionsHeight ){
			$(this).height(landingActionsHeight);
			$(this).css({'position':'relative'});
			$('.button', this).css({'position':'absolute', 'bottom':'25px', 'left':'15px'});
		}
	});
	$('#landingActions div.col.videos').css('height', 'auto');
	
	// check if red content bit is empty, if so add a class.
	$('#contentSpecialInner:empty').addClass('csInnerEmpty');
	$('.pageRowInner').css({'zoom':1}); // then set zoom just to make IE7 happy!
	
	// landing page forms - messages
	// landing actions
	$('.landing #landingActions .form').each(function(){
		if($('.message', this).text() != ''){
			$('.block', this).hide();
			$('.message', this).animate({ opacity:1 }, 3500, null, function(){
				$(this).fadeOut('slow', function(){
					$(this).siblings('.block').fadeIn('fast');
				});
			});
		}
	});
	// corporate landing page
	$('.landing #corpLandingActions .form').each(function(){
		if($('.message', this).text() != ''){
			$('.block', this).hide();
			$('.message', this).animate({ opacity:1 }, 3500, null, function(){
				$(this).fadeOut('slow', function(){
					$(this).siblings('.block').fadeIn('fast');
				});
			});
		}
	});
	// subContent
	$('.landing .subContentCols .form').each(function(){
		if($('.message', this).text() != ''){
			$('.content', this).hide();
			$('.message', this).animate({ opacity:1 }, 3500, null, function(){
				$(this).fadeOut('slow', function(){
					$(this).siblings('.content').fadeIn('fast');
				});
			});
		}
	});
	
	// home page col hovers
	$('.home .col').hover(function(){
		$(this).addClass('homeColHover');
	}, function(){
		$(this).removeClass('homeColHover');
	});
	
	
});

function facebook_share() {
    window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(location.href)+'&t='+encodeURIComponent(document.title),'sharer','resizable=1,scrollbars=1,toolbar=0,status=0,width=626,height=436');
    return false;
}
    
function digg_share() {
    window.open('http://digg.com/submit?phase=2&url='+encodeURIComponent(location.href),'sharer','toolbar=0,status=0,width=626,height=436');
    return false;
}

function delicious_share() {
    window.open('http://del.icio.us/post?v=2&url='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title),'sharer','toolbar=0,resizable=1,scrollbars=1,status=0,width=626,height=436');        
    return false;
}

function stumbleupon_share() { 
    window.open('http://www.stumbleupon.com/submit?url='+encodeURIComponent(document.URL)+'&titlet='+encodeURIComponent(document.title),'sharer','toolbar=0,resizable=1,scrollbars=1,status=0,width=626,height=436'); 
    return false;
}
    
    

function openWindow(url, width, height){
	
	var strWinName = "ambo";
	var fixHeight = 28;
	var fixWidth = 8;
	if(width ==  undefined || width == "" ) {
		width = screen.availWidth-fixWidth;
		height = screen.availHeight-fixHeight;
	}
	var strParams = "";
	if (navigator.appName.indexOf("Netscape") != -1) {
		strParams += "screenX=0,screenY=0";
	} else {
		strParams += "left=0,top=0,fullscreen=0";
	}
	strParams += ",width="+(width)+",height="+(height)+",scrollbars=no,menubar=no,resizable=yes,toolbar=no,location=no,status=no"
	newWindow = window.open(url, strWinName, strParams);
			
	if (newWindow) {
		newWindow.focus();	
		newWindow = null;
	}
}

