$(function(){
	// vertically align function
	$.fn.vAlign = function() {
		return this.each(function(i){
		var ah = $(this).height();
		var ph = $(this).parent().height();
		var mh = (ph - ah) / 2;
		$(this).css('margin-top', mh);
		});
	};
});

/* call jquery on ready */
$(document).ready(function(){
	// valign container
	$("#container").vAlign(); 
	createCookie('cont_top',$("#container").css("margin-top"),7);
	 
	$('#box_bg').pngFix();
	$('#logo').pngFix();
	
	$("#content-slider").slider({
		orientation: "vertical",
		change: handleSliderChange,
		min: -100,
		max: 0,
		slide: handleSliderSlide
	});

	$("form").submit(function() {
		$("#submit").html('<img src="images/template/ajax-loading.gif" alt="loading" height="16">');
    });

	$("#nav_ul li, #sub_nav li").hover(function(){
		if($(this).attr('id').search('_active') == -1){
			$(this).removeClass($(this).attr("id"));
			$(this).addClass($(this).attr("id") + '_active');
		}
	}, function (){
		if($(this).attr('id').search('_active') == -1){
			$(this).removeClass($(this).attr("id") + '_active');
			$(this).addClass($(this).attr("id"));
		}          
	});
});

/* call jquery onload */
$(window).bind("load", function() { 
	//add text slider  
	var scrollHeight =  $('#content-scroll').innerHeight();
	var itemHeight =  $('#content-holder .content-item').innerHeight();
	//alert( itemHeight + ' > ' + scrollHeight )
	if ( $("#content-holder .content-item").length > 0 && ( ( itemHeight + 25 ) > scrollHeight ) ) {
		$("#content-slider-bg").css({'display' : 'block' });
		$("#content-slider").css({'display' : 'block' });
	}
	// add picture slider
	$("div#slide").slideView({ 
		easeTime: 1200 
	});
}); 

/* call jquery on resize */
$(window).bind('resize', function(){
	$("#container").vAlign();
	createCookie('cont_top',$("#container").css("margin-top"),7);
});

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function handleSliderChange(e, ui)
{
  var maxScroll = $("#content-scroll").attr("scrollHeight") - $("#content-scroll").height();
  $("#content-scroll").animate({scrollTop: -ui.value * (maxScroll / 100) }, 1000);
}

function handleSliderSlide(e, ui)
{
  var maxScroll = $("#content-scroll").attr("scrollHeight") - $("#content-scroll").height();
  $("#content-scroll").attr({scrollTop: -ui.value * (maxScroll / 100) });
}