$(document).ready(function(){
	initFontSize();
	fixIEzindex();
	initOverlay();
	$('.scrolling').jScrollPane({showArrows: true});
	$("a[rel^='prettyPhoto']").prettyPhoto({theme:'light_square'});
	$(".btn-print>a").click(function() {window.print(); return false;});
});

function initOverlay() {
	//$("a[rel^='prettyPhoto']").prettyPhoto();
	//$("a[rel^='google-map']").prettyPhoto({theme:'light_square'});
	$('.overlay').each(function() {
		if ($(this).attr("rel") == "google-map") {
			$(this).attr("print", googleMapPrintUrlReplace($(this).attr("href")));
			$(this).attr("href", $(this).attr("href")+'&output=embed&iframe=true&width=600&height=400');
			$(this).prettyPhoto({theme:'light_square'});
		} else if ($(this).attr("rel") == "iframe") {
			$(this).attr("print", $(this).attr("href")+'?printed=true');
			$(this).attr("href", $(this).attr("href")+'&iframe=true&width=720&height=600');
			$(this).prettyPhoto({theme:'light_square'});
		} else if ($(this).attr("rel") == "video") {
			$(this).attr("print", $(this).attr("href")+'?printed=true');
			$(this).attr("href", $(this).attr("href")+'&iframe=true&width=500&height=375');
			$(this).prettyPhoto({theme:'light_square'});
		} else if ($(this).attr("rel") == "prettyPhoto[pp_gal]") {
			$(this).attr('id', 'no-print-btn');
			$(this).attr("print", $(this).attr("href"));
			$(this).prettyPhoto({theme:'light_square'});
		} else if ($(this).attr("rel") == "prettyPhoto[pp_gal_cr]") {
			$(this).attr('id', 'no-print-btn');
			$(this).attr("print", $(this).attr("href"));
			$(this).prettyPhoto({theme:'light_square'});
		} else if ($(this).attr("rel") == "prettyPhoto[pp_gal_sh]") {
			$(this).attr('id', 'no-print-btn');
			$(this).attr("print", $(this).attr("href"));
			$(this).prettyPhoto({theme:'light_square'});
		} else if ($(this).attr("rel") == "prettyPhoto[pp_gal_caption]") {
			$(this).attr('id', 'no-print-btn');
			$(this).attr("print", $(this).attr("href"));
			$(this).prettyPhoto({theme:'light_square'});
		} else {
			$(this).attr("print", $(this).attr("href"));
			$(this).prettyPhoto({theme:'light_square'});
		}
	});
}

function fixIEzindex() {
	//Fix IE7 z-index
	var zIndexNumber = 1000;
	$('.fix-z').each(function() {
		$(this).css('zIndex', zIndexNumber);
		zIndexNumber -= 10;
	});
}

function initFontSize() {
	var maxFontSize = 16;
	var minFontSize = 10;
	
	var cookieFontSize = getCookie("New_Font_Size");
	var originalFontSize = $('#post-body').css('font-size');	
	if (cookieFontSize != "") {
		if ((cookieFontSize+"px") != originalFontSize) {
			$('#page-content, #page-content p, #page-content h3, #page-content h4, #page-content h5').css('font-size', (cookieFontSize+"px"));
		}		
	}
	
	// Increase Font Size
	$(".increaseFont").click(function(){
		var currentFontSize = $('#page-content').css('font-size');
		var currentFontSizeNum = parseFloat(currentFontSize, 10);
		var newFontSize = currentFontSizeNum + 2;
		if (newFontSize > maxFontSize) {
			newFontSize = maxFontSize;
		}
		setCookie("New_Font_Size", newFontSize,2);
		$('#page-content, #page-content p, #page-content h3, #page-content h4, #page-content h5').css('font-size', newFontSize);
		return false;
	});
	// Decrease Font Size
	$(".decreaseFont").click(function(){
		var currentFontSize = $('#page-content').css('font-size');
		var currentFontSizeNum = parseFloat(currentFontSize, 10);
		var newFontSize = currentFontSizeNum - 2;
		if (newFontSize < minFontSize) {
			newFontSize = minFontSize;
		}
		setCookie("New_Font_Size", newFontSize,2);
		$('#page-content, #page-content p, #page-content h3, #page-content h4, #page-content h5').css('font-size', newFontSize);
		return false;
	});
}

function setCookie(c_name,value,expiredays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toUTCString())+  "; path=/";
}

function getCookie(c_name) {
if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) {
		c_start=c_start + c_name.length+1;
		c_end=document.cookie.indexOf(";",c_start);
		if (c_end==-1) c_end=document.cookie.length;
		return unescape(document.cookie.substring(c_start,c_end));
	}
}
	return "";
}

function alertSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  //window.alert( 'Width = ' + myWidth );
  window.alert( 'Height = ' + myHeight );
}

function googleMapPrintUrlReplace(url) {
	var ll = getQueryString(url, 'll');
	var newurl = 'http://maps.google.com/maps/api/staticmap?center='+ll+'&zoom=19&size=1024x600&&markers=icon:http://www.hanglung.com/images/button_point_map.png|'+ll+'&sensor=true';
	return newurl;
}

function getQueryString(url, str) {
	var gy = url.split("&");
	for (i=0;i<gy.length;i++) {
		ft = gy[i].split("=");
		if (ft[0] == str) {
			return ft[1];
		}
	}
}
