$(document).ready(function(){

	/*************************************************************************/
	/*----------------------------- SEARCH ----------------------------------*/ 
	
	$(".search_body").hide();
	$(".search_head").click(function(){
		$(this).next().animate({width: 'toggle'});
	});
	
	/*************************************************************************/
	/*----------------------------- HEADER NAV DROPDOWNS --------------------*/ 
	
	$('#header_navlist ul').superfish({ 
        delay: 200,									// delay on mouseout 
        animation: {opacity:'show'},				// fade-in and slide-down animation 
        speed: 400,									// faster animation speed 
        autoArrows: true,							// generation of arrow mark-up 
        dropShadows: false,							// disable drop shadows 
		disableHI: false							// hover intent is disabled	
    }); 
	
	/*************************************************************************/
	/*----------------------------- NIVO SLIDER -----------------------------*/ 
	
	$('#slider').before('<div class="nivo_tl"></div>');
	$('#slider').before('<div class="nivo_tr"></div>');
	$('#slider').before('<div class="nivo_bl"></div>');
	$('#slider').before('<div class="nivo_br"></div>');
	
	/*************************************************************************/
	/*----------------------------- MESSAGE ---------------------------------*/ 
	
	var msgwraph = $(".msg").height();
	$('.msg_button').wrap('<div class="msg_button_wrap fr"></div>');
	$('.msg_button_wrap').css("height", msgwraph+"px").css("display", "table");
	
	/*************************************************************************/
	/*----------------------------- ACCORDIAN - OPEN/CLOSE ------------------*/ 
	
	$(".acc_item_1").width(320);

	lastBlock = $(".acc_item_1");
    maxWidth = 320;
    minWidth = 75;	
    $(".acc_item").mouseover(function(){
        $(lastBlock).animate({width: minWidth+"px"}, { queue:false, duration:400 });
		$(this).animate({width: maxWidth+"px"}, { queue:false, duration:400});
		lastBlock = this;
    });
	
	/*************************************************************************/
	/*----------------------------- ACCORDIAN - SCROLL ----------------------*/ 

	$(function() {
		var step = 410; 
		var current = 0; 
		var maximum = $('#acc ul li').size(); 
		var visible = $("#acc").width();
		var speed = 600; 
		var liSize = (((75+24)*maximum)+245)/maximum; //320-75=245 EXTRA WIDTH FOR AN OPEN ACCORDIAN ITEM // 24= left+right padding
		
		var ulSize = liSize * maximum;

		$('#acc ul').css("width", ulSize+"px").css("left", 0).css("position", "absolute");
 
		$('.acc_next').click(function() { 
			if( current + visible + step > ulSize) {
				if (current + visible < ulSize) {
					current = ulSize - visible;
					$('#acc ul').animate({left: -(current)}, speed, null);
				} else {return;}
			}else {
				current = current + step;
				$('#acc ul').animate({left: -(current)}, speed, null);
			}
			return false;
		});
 
		$('.acc_prev').click(function() { 
			if( current < step) {
				if (current!=0) {
					current = 0;
					$('#acc ul').animate({left: 0}, speed, null);
				} else {return;}
			}else {
				current = current - step;
				$('#acc ul').animate({left: -(current)}, speed, null);
			}
			return false;
		});
	});
	
	/*************************************************************************/
	/*----------------------------- HTML5 VIDEO -----------------------------*/ 
	
	VideoJS.setupAllWhenReady({
		controlsBelow: false, // Display control bar below video instead of in front of
		controlsHiding: true, // Hide controls when mouse is not over the video
    	defaultVolume: 0.85, // Will be overridden by user's last volume if available
	    flashVersion: 9, // Required flash version for fallback
    	linksHiding: true // Hide download links when video is supported
	});
	
	/*************************************************************************/
	/*----------------------------- TABS ------------------------------------*/ 
	
	$('.list-wrap .tabpanel:first-child').removeClass("hide");
	
	jQuery('.tabset').each(function() {
		var this_id = jQuery(this).attr('id');
		$("#"+this_id).organicTabs({"speed": 200});
	});
	
	/*************************************************************************/
	/*----------------------------- TOGGLES ---------------------------------*/ 
	
	jQuery('.toggleboxhead').each(function() {
		var this_id = jQuery(this).attr('id');
		var cn=0;
		$(this).click(function() {
  			jQuery('.'+this_id+' .toggleboxcon-in').fadeToggle('slow');	// added instead of slideToggle
			//jQuery('.'+this_id).slideToggle('slow');					// slideToggle - jerky problem (due to non-specified 'height' in css). Hence this is replace with code below:
			jQuery(this).toggleClass('toggleboxheadactive');
			var toggleconheight = $('.'+this_id+' .toggleboxcon-in').height();
			if (cn==0) 	{ $("."+this_id).animate({ height: toggleconheight+20 }); cn=1;}	//20=10+10 for padding in style.css
			else		{ $("."+this_id).animate({ height: 0 }); cn=0;}
		});
		
	});
	
	/*************************************************************************/
	/*----------------------------- SCROLLTO --------------------------------*/ 
	
	jQuery('.gototop').click(function() {
		var destination = $('body').offset().top;
		$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, 500 );
	});
	var scrollcustom= document.URL;
	var scrollcustomuri = scrollcustom.split("#scrollto:");
	if (scrollcustomuri[1]) {
		var dest = $('#'+scrollcustomuri[1]).offset().top;
		$("html:not(:animated),body:not(:animated)").animate({ scrollTop: dest}, 500 );
	}
	
	jQuery('.scrollto').click(function() {
		var target = jQuery(this).attr('id');
		var destin = $('#scrolled_'+target).offset().top;
		$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destin}, 500 );
	});
	
});


$(window).load(function() {

	/*************************************************************************/
	/*----------------------------- HEADER NAV BG ---------------------------*/ 

	if ($("#slide").length > 0){					// important for bug fixin IE8
		menuSlider.init('header_navlist','slide'); 	/* must be initialized with '$(window).load' instead of '$(document).ready' */
	}
	
});
