﻿// JavaScript Document

var animtype = 'fade1';
var OverLap = 35;
var moveHowFar = 30;
var AnimationTime = 300;
var tmid;
var animatetime = 7500;

$(document).ready(function(){
	if ($("#fbcont").length){
		setTimeout(function(){ fbwidth(); },500);		// hide facebook added iframe that is too long 
	}
	var fadetime = 1000; // tabs animation 
	var $tabs = $('#tabs_headers DIV');
	if ($tabs.length){	
		var tabswidth = Math.floor( ($('#tabs').width() ) / ($tabs.length))-((($('#tabs').width() ) % ($tabs.length))+4);
		$tabs.css({'width':tabswidth+'px'});
		var $tabs_content = $('#tabs .tab_content');
		var tabs_content_height = 0;       
		$tabs_content.each(function() {
			if ($(this).innerHeight() > tabs_content_height){
				tabs_content_height = $(this).innerHeight();
			}
		});
		$('#tabs').height(tabs_content_height+ $tabs.innerHeight());
		$tabs_content.height(tabs_content_height+5);
		$tabs_content.css({'width':((tabswidth*$tabs.length))+'px'});
		$tabs.click( function(){
			$tabs.removeClass('on');
			$(this).addClass('on');           
			var clickid=$(this).attr('id');
			if (animtype == 'fade'){
				$tabs_content.stop(true).animate({opacity: 0},(Math.floor(fadetime/2)),function(){
					$tabs_content.hide();
					$("#"+clickid+"_content").show();
					$("#"+clickid+"_content").stop(true).animate({opacity: 1},(Math.floor(fadetime/2)));
					return false;
				});
			} else {
				$tabs_content.stop(true).animate({height: 0},(Math.floor(fadetime/2)),function(){
					$tabs_content.hide();
					$("#"+clickid+"_content").show();
		
					$("#"+clickid+"_content").stop(true).animate({'height':tabs_content_height+'px'},(Math.floor(fadetime/2)),'swing');
					return false;
				});
			}
		});
	}	// end of tabs animation
	
	var $icons = $('#socialicons DIV');		//facebook, twitter, etc. icons animation
	if ($icons.length){
		var j = 0;
		var i = $icons.length;
		$icons.each(function(){
			$(this).css({'right': j+'px'});
			$(this).css({'z-index':i*100});
			$(this).bind('mouseover',function(){
				var leftp = $(this).css('right');
				leftp = parseInt(leftp.substr(0,leftp.indexOf('px')));
				leftp = leftp + moveHowFar;
				$(this).stop(true).animate({'right':leftp+'px'},AnimationTime);
				$(this).bind('mouseleave',function(    ){
					var leftp = $(this).css('right');
					leftp = parseInt(leftp.substr(0,leftp.indexOf('px')));
					leftp = leftp - moveHowFar;
					$(this).stop(true).animate({'right':leftp+'px'},AnimationTime,function(){
						$(this).unbind('mouseleave');
						reseticons();
					});
				});
			});
	
			j += -OverLap;
			i--;
		});
	} // end of facebook, twitter, etc. animation

	if ($(".main_image2").length){
		
		$(".main_image2").hover( function(){ clearTimeout(tmid); }, function () { setTimeout(function(){ tmid = animate();  },animatetime); });
		
		$(".main_image2 .desc").show(); //Show Banner
		$(".main_image2 .block").animate({ opacity: 0.90 }, 1 ); //Set Opacity
		$(".image_thumb ul li:first").addClass('active'); //Add the active class (highlights the very first list item by default)
		$(".image_thumb ul li").bind('click',function(){
			clearTimeout(tmid);
			//Set Variables
			var imgAlt = $(this).find('img').attr("alt"); //Get Alt Tag of Image
			var imgTitle = $(this).find('a').attr("href"); //Get Main Image URL
			//alert(imgTitle);
			var imgDesc = $(this).find('.block').html();  //Get HTML of the "block" container
			var imgDescHeight = $(".main_image2").find('.block').height(); //Find the height of the "block"
		
			if ($(this).is(".active")) {  //If the list item is active/selected, then...
				return false; // Don't click through - Prevents repetitive animations on active/selected list-item
			} else { //If not active then...
				//Animate the Description
					$(".main_image2 .collapse").fadeToggle();
					$(".main_image2 .miprev").fadeToggle();
					$(".main_image2 .minext").fadeToggle();
				$(".main_image2 .block").animate({ opacity: 0, marginBottom: -imgDescHeight }, 250 , function() { //Pull the block down (negative bottom margin of its own height)																													 
					$(".main_image2 .block").html(imgDesc).css({'margin-top':'-10px'}).delay(1500).animate({ opacity: 0.90,  marginBottom: "0" }, 250, function (){
					
						$(".main_image2 .collapse").fadeToggle();	
						$(".main_image2 .miprev").fadeToggle();
						$(".main_image2 .minext").fadeToggle(); 
					}); //swap the html of the block, then pull the block container back up and set opacity
					$(".main_image2 img").attr({ src: imgTitle , alt: imgAlt}); //Switch the main image (URL + alt tag)
				});
			}
			//Show active list-item
			$(".image_thumb ul li").removeClass('active'); //Remove class of 'active' on all list-items
			$(this).addClass('active');  //Add class of 'active' on the selected list
			return false;
		
		}) .hover(function(){ //Hover effects on list-item
			$(this).addClass('hover'); //Add class "hover" on hover
			}, function() {
			$(this).removeClass('hover'); //Remove class "hover" on hover out
		});
		$(".collapse").click(function(){
			$(".main_image2 .block").slideToggle(); //Toggle the description (slide up and down)
			$(".collapse").toggleClass("show"); //Toggle the class name of "show" (the hide/show tab)
		});
		
		$(".main_image2 .desc").append('<div class="miprev">Previous</div>');
		$(".main_image2 .desc").append('<div class="minext">Next</div>');
		$(".main_image2 .minext").bind('click',function(){
			clearTimeout(tmid);
			//alert('clicked next');
			var $imgthumb1 = $(".image_thumb ul li");
			var len1 = $imgthumb1.length;
			var found1 = false;
			$imgthumb1.each(function(index){			
				if ($(this).hasClass('active') && !found1){
					if (index == (len1-1)){
						$imgthumb1.first().trigger('click');				
					} else {
						$(this).next().trigger('click');
					}
					found1 = true;
				}
			});
			//
			/*tmid = animate();*/
		});
		$(".main_image2 .miprev").bind('click',function(){
			clearTimeout(tmid);
			//alert('clicked next');
			var $imgthumb1 = $(".image_thumb ul li");
			var len1 = $imgthumb1.length;
			var found1 = false;
			$imgthumb1.each(function(index){			
				if ($(this).hasClass('active') && !found1){
					if (index == 0){
						$imgthumb1.last().trigger('click');						
					} else {
						$(this).prev().trigger('click');
					}
					found1 = true;
				}
			});
			//tmid = animate();
		});
		
		tmid = animate();
	}
	if ($("#mrgdownload").length){ 
		$("#mrgdownload").css({
			'position'	: 'relative',
			'top'		: '230px',
			'left'		: '230px',
			'display'	: 'block',
			'width'		: '185px',
			'height'	: '40px',
			'cursor'	: 'pointer'
		}).bind('click',function(){
			window.open($(this).children('A').attr('href'));
		});
	}
});
function animate(){
	var $imgthumb = $(".image_thumb ul li");
	var len = $imgthumb.length;
	var found = false;

	$imgthumb.each(function (index){
	if ($(this).hasClass('active') && !found){
		if (index == (len-1)){
			$imgthumb.first().trigger('click');				
		} else {
			$(this).next().trigger('click');
		}
		found = true;
	}
	});
	
	return setTimeout(function(){ tmid = animate();  },animatetime);
}

function reseticons(){
    var $icons2 = $('#socialicons DIV');
    var j = 0;
    var i = $icons2.length;
    $icons2.each(function(){
        $(this).css({'right': j+'px'});
        $(this).css({'z-index':i*100});
        j += -OverLap;
        i--;
    });
}
function fbwidth(){
	var $fb = $("#fbcont iframe");
	if ($fb.length){
		$fb.each(function(){
			if ($(this).width() > 100){
				$(this).css({'display':'none'});
			}
		});
	} else {
		setTimeout(function (){fbwidth();},500)
	}
}
