/* 

functions.js 

Written by Tyler Magee for MCG - Diagram Building solutions 

September 2011 

*/

jQuery(document).ready(function($) {

var viewport = $('meta[name="viewport"]');
var nua = navigator.userAgent;
    if ((nua.match(/iPad/i)) || (nua.match(/iPhone/i)) || (nua.match(/iPod/i))) {
        viewport.attr('content', 'width=device-width, minimum-scale=1.0, maximum-scale=1.0');
    $('body')[0].addEventListener("gesturestart", gestureStart, false);
    }
    function gestureStart() {
        viewport.attr('content', 'width=device-width, minimum-scale=0.25, maximum-scale=1.6');
    }
});

$(document).ready( function () {

	mainGallery();

	ml = $(window).width();
	
	var cs = 0;
	
	$(".slider-controls").each (function () {
		cs++;
	});
	
	if (cs > 1) {
	
		sliderInit();
	
	}
	
	initLB();
	
	accordianContent();
	flyouts();
	newsFlyouts();
	$(document).pngFix(); 
});

var liltimer;

function initLB () {
	
	var num = 0;
	var gallery = 0;
	
	$(".enlarge-image").each( function () {
		
		var pl = $(this).attr("src");
		var cl = $(this).attr("class");
		var targ = $(this).offsetParent();
		
		$("<a href='"+pl+"' rel='photo-single-"+num+"'><img src='"+pl+"' class='single-enlarge "+cl+"' /></a>").insertAfter(this);
		$(this).css("display", "none");
		$("a[rel='photo-single-"+num+"']").colorbox();
		num++;
		
	});
	
	$(".slider-gallery").each ( function () {
		
		num = 0;
		gallery++;
		var pos = 0;
		var dw = $(this).innerWidth();
		
		$(this).addClass('slider-inner-'+gallery);
		
		var targ = "mini-gallery-";
		
		$(".slider-gallery img").each ( function () {
			
			$("br").each( function () {
				//alert('line break');
				$(this).detach();
			});

			var dh = $(this).innerHeight();
			
			if (num == 0) {
				$(this).addClass(targ+num);
			} else {
				$(this).css("left", dw+"px").addClass(targ+num);
			}
			
			pos += dw;
			num++;			
			
		});
		
		liltimer = setTimeout("shift('slider-inner-"+gallery+"')", 5000);
		
	});
	
	
	
}

function shift (target) {

	clearTimeout(liltimer);

	var ml = $("."+target+" img").eq(0).innerWidth();
	var n = $("."+target+":last-child").css('left')+ml;
	
	$("."+target+" img").eq(0).animate({
		left: "-="+ml+"px"
	}, 750, function () {
		$(this).css("left", ml);
		$("."+target).append($("."+target+" img").eq(0));
	});

	$("."+target+" img").eq(1).animate({
		left: "0px"
	}, 750, function() {
		liltimer = setTimeout("shift('"+target+"')", 5000)
	});

}

$(window).resize( function () {

	ml = $(window).width();
	
	$("#home_image").css("width", ml+"px");
	
	var i = 1;
	
	$(".slider-controls").each(function () {
		
		var pos = $("#slider-"+i).css("left");
		$("#slider-"+i).css("left", (ml*(i-1))+"px");
		i++;
	});
	
	$(".home-master-slider").each( function () {
		
		var cp = $(this).css('left');
		
		if (cp != '0px') {
			$(this).css('left', $(window).width()+"px");
		}
		
	});
	
});

var current = 0;
var msca = new Array();

var timer;
var t = 2000;
var ml = 0;

function sliderInit () {
	
	var i = 0;
	
	// Re-display nav controls
	
	$("#ms-left").fadeIn('1750');
	$("#ms-right").fadeIn('1750');
	$("#slider-control").fadeIn('1750');
	
	$("#home_image").css("width", ml+"px");
	
	$("#slider-control-0").addClass('hit');
	
	$(".slider-controls").each(function () {
			
		$("#slider-"+i).css("left", (ml*i)+"px");
		
		var mi = i;
		msca.push(mi);
		i++;
		
		$(this).click(function (e) {
			e.preventDefault();
			
			var d = $(this).attr("href");
			
			//alert( "d is "+d+" current slide is "+current);
			ml = $(window).width();
			
			if ($("#main-slide").is(":animated") != true && current != d) {
				
				$('.hit').removeClass('hit');
				
				if (current < d) {
					
					 //*(d - current);
					$("#main-slide").animate({
						left: '-='+((d-current)*ml)+"px"
					}, t, "easeInOutExpo", function () {				
						$("#slider-control-"+d).addClass("hit");
						current = d;
						//alert("current is "+current);
					});
					
				} else if (current > d) {
	
					 //*(current - d);
					
					$("#main-slide").animate({
						left: '+='+((current-d)*ml)+"px"
					}, t, "easeInOutExpo", function () {						
						$("#slider-control-"+d).addClass("hit");
						current = d;
						//alert("current is "+current);
					});
					
					
				}
									
				current = d;
					
			}
			
		});
		
		
	}); // End SLN function
	
	$('#slide-back').click(function (e) {
		e.preventDefault();
		
		$('.hit').removeClass('hit');
		
		if (current != 0) {
			// Move "Left"
			current--;
			$("#main-slide").animate ({
				left: '+='+ml+"px"
			}, t, "easeInOutExpo", function () {
				$("#slider-control-"+current).addClass('hit');
			});
			
		} else if (current == 0) {
			// Reset
			current = msca.length-1;
			$("#main-slide").animate ({
				left: "-="+ml*current+"px"
			}, t, "easeInOutExpo", function () {
				$("#slider-control-"+current).addClass('hit');
			});
			
		}
		
	});
	
	$('#slide-next').click(function (e) {
		e.preventDefault();
		
		$('.hit').removeClass('hit');
		
		current++;		

		if (current < msca.length) {
			// Move "Right"
			$("#main-slide").animate ({
				left: '-='+ml+"px"
			}, t, "easeInOutExpo", function () {
				$("#slider-control-"+current).addClass('hit');
			});
			
			
		} else if (current >= msca.length) {
			// Reset
			current = 0;
			$("#main-slide").animate ({
				left: "0px"
			}, t, "easeInOutExpo", function () {
				$("#slider-control-"+current).addClass('hit');
			});
			
		} 
			
	});
	
	timer = setTimeout('phase()', 5000);
	
}

function phase () {
	
	current++;
	clearTimeout(timer);
	
	$(".hit").removeClass('hit');
	
	if (current < msca.length) {
		// Move "Right"
		$("#main-slide").animate ({
			left: '-='+ml+"px"
		}, t, "easeInOutExpo", function () {
			$("#slider-control-"+current).addClass('hit');
			timer = setTimeout('phase()', 5000);
		});
		
	} else if (current >= msca.length) {
		// Reset
		current = 0;
		$("#main-slide").animate ({
			left: "0px"
		}, t, "easeInOutExpo", function () {
			$("#slider-control-"+current).addClass('hit');
			timer = setTimeout('phase()', 5000);
		});
		
	} 
	
}

var mainTimer;

function mainGallery () {
	
	var num = 0;
	var gallery = 0;
	
	$("#diagram-master-slide").each ( function () {
		
		num = 0;
		gallery++;
		var pos = 0;
		
		var mw = $(this).innerWidth();
		//alert(mw);
		
		var targ = $(this).attr('id');
		
		$(".home-master-slider").each( function () {
			
			var dw = $(this).innerWidth();
			var dh = $(this).innerHeight();
			
			$(this).addClass('diagram-master-slide-'+num);
							
			var msg;
			
			if (num > 0) {
				$(this).css("left", dw+"px");
			}
			
			pos += dw;
			num++;			
			
		});
		
		mainTimer = setTimeout("mainMove('"+$(this).attr('id')+"')", 5000);

	});

}

function mainMove (ta) {
	
	clearTimeout(mainTimer);
	
	var ml = $("#"+ta+"> div").innerWidth();
	var mh = $("#"+ta+"> div").innerHeight();
	var n = $("#"+ta+":last-child").css('left')+ml;
	
	//alert('it\'s doing something...');
	
	$("#"+ta+" div").eq(0).animate({
		left: "-="+ml+"px"
	}, 1750, "easeInOutExpo", function () {
		$(this).css("left", ml);
		$("#"+ta).append($("#"+ta+" div").eq(0));

		mainTimer = setTimeout("mainMove('"+ta+"')", 5000);
	});

	$("#"+ta+" div").eq(1).animate({
		left: "0px"
	}, 1750, "easeInOutExpo");
	
	
}

function accordianContent () {
	
	var ac = 0;
	
	$('.accordian').each( function () {
		ac++;
		var ON = $(this).attr('touse');
		var OFF = $(this).attr('toclose');
		var MH = 0;//$(this).css('height');
		
		$(this).attr('id', 'accordian-collapse-'+ac).before("<a href=\"#\" class='collapse-"+ac+"' control=\"accordian-collapse-"+ac+"\" toggle='on'>"+ON+"</a>").css('height', '0px').css('display', 'block');
		
		$('.collapse-'+ac).click( function (e) {
			//alert($(this).attr('control'));
			e.preventDefault();
			var tc = $(this).attr('control');
			if (MH == 0) {
				$("#"+tc+" > *").each( function () {
					//alert($(this).html());
					MH += $(this).outerHeight()+($(this).outerHeight()*0.3);
					//alert (MH + " from: "+$(this).outerHeight());
				});
			}
			
			if ($(this).attr('toggle') == 'on' ) {
				$(this).attr('toggle', 'off').html(OFF);
				$("#"+tc).animate({
					height: MH+"px"
				}, 1000, "easeInOutExpo");
				
			} else {
				$(this).attr('toggle', 'on').html(ON);
				$("#"+tc).animate({
					height: '0px'
				}, 1000, "easeInOutExpo");
				
			}
			
		});
		
	});

}

function flyouts() {
	
	var fid = 'flyout-control-';
	var num = 0;
	
	$('.explainer').each( function () {
		//alert( $(this).html() );
		num++;
		$(this).attr('id', fid+num);
		var fidc = "#"+$(this).attr('id');
		
		$(this).hover( function() {
			$(fidc+' .page-flyout').stop('true').animate({
				left: '0px'
			}, 750, "easeInOutExpo");
		}, function() {
			$(fidc+' .page-flyout').stop('true').animate({
				left: "-"+$('.page-flyout').outerWidth()+"px"
			}, 750, "easeInOutExpo");
		});

	});
	
}

function newsFlyouts() {
	var nfid = 'news-flyout-control-';
	var num = 0;
	
	$('.more-news').each( function () {
		//alert( $(this).html() );
		num++;
		$(this).attr('id', nfid+num);
		//alert( $(this).attr('id') );
		var nfidc = "#"+$(this).attr('id');
		
		$(this).hover( function() {
			$(nfidc+' .news-flyout').stop('true').animate({
				left: '0px'
			}, 750, "easeInOutExpo");
		}, function() {
			$(nfidc+' .news-flyout').stop('true').animate({
				left: $('.news-flyout').outerWidth()+"px"
			}, 750, "easeInOutExpo");
		});

	});
	
}

