// Gallery Combination Of 
// http://papermashup.com/simple-jquery-gallery/ 
// This sets a fancy fade in on load
// Ethan Hackett 2011


// Compressed Using http://www.minifyjavascript.com/


//Homepage News Slider
//$(document).ready(function() {
//	setSlider('#grid');
//});

function t1() {
    $("#t1-t1").load("/usercontent/homepage/featured-tabs/t1-t1.html");
   	$("#t1-t1").css('z-index', 17900 );
}   	
function t2() {
   	$("#t1-t2").load("/usercontent/homepage/featured-tabs/t1-t2.html");
   	$("#t1-t2").css('z-index', 17901 );
}
function t3() {
	$("#t1-t3").load("/usercontent/homepage/featured-tabs/t1-t3.html");
	$("#t1-t3").css('z-index', 17902 );  
}
function t4() {	
	$("#t1-t4").load("/usercontent/homepage/featured-tabs/t1-t4.html");
	$("#t1-t4").css('z-index', 17903 );
}
function t5() {	
	$("#t1-t5").load("/usercontent/homepage/featured-tabs/t1-t5.html");
	$("#t1-t5").css('z-index', 17904 );
}

//Hide Google Only Content
$(function() {
	$( "#tb61" ).slideToggle('1');
	$( "#tb219" ).slideToggle('1');
	$( "#tb302" ).slideToggle('1');
	$( "#tb441" ).slideToggle('1');
	
	// Set's the last link in the portfolio navigation to have a class removing the right bar.
	$('#portfolioNav li:last').addClass('no-right-bar');
	
});


// This is the script that sets the active class in the menu.
$(function() {
	// Assign the page URL as a  veriable starting after .com/...
	var activeLinkPortfolio = window.location.pathname.split('/')[2];
	//alert(activeLinkPortfolio);
	// Find the Li with an # that matches the portion of the gallery then assign it the active class.
	$("li#" + activeLinkPortfolio +" a").addClass('portfolioNavActive');
	
	var designactive = window.location.pathname.split('/')[1];
	if ( designactive == "design-portfolio.aspx") {
		$("li#web-design-portfolio a").addClass('portfolioNavActive');
	};
	
});


// Sets fade in and sets focus
$(function() {
	$('.image').animate({'opacity' : '0.3'});
	$('#scroll-content img').animate({'opacity' : '1.0'});
	$('#gallerylinks').children(':first').fadeTo('slow', 1, function(){
		$(this).addClass("active-thumb");
		
		//Define the veriable activeThumb for use with the arrows (+1 to start at 1 and prev = 0)
		var activeThumb = $('#gallerylinks').children('a').index(this) + 1;
		
	});
	$('#scroll-content img').animate({'opacity' : '1.0'});
	
	//  Solved the premature triggering of the setSlider script
	//	I used the .bind('load') method to check once the image is loaded
	//	Once the image is loaded then it fires the setSlider function
	//	Pain in my but!!!
		
	$('#scroll-content img').bind('load', function() {
		setSlider('#scroll-pane');
	});

	//Next > Arrow
	$("#gallery-next-image").live('click', function() {
			
			//Define the veriable activeThumb for use with the arrows (+1 to start at 1 and prev = 0)
			var activeThumb = $('.active-thumb').index('#gallerylinks a') + 1;
			//alert("Arrow " + activeThumb + "");
			
			// Get Number of list items
			var numThumb= $('#gallerylinks a').length;
			
			
			//This checks to see if the current clicked thumb is the first
			if(activeThumb == 1) {
				//alert("Im First " + activeThumb + "");
				 $("body").find('#gallerylinks a').eq(activeThumb).trigger('click');	
			}
			//This checks to see if the current clicked thumb neither the first or last
			else if (activeThumb < numThumb){
				//alert("Im in the middle " + activeThumb + "");
				$('body').find('#gallerylinks a').eq(activeThumb).trigger('click');
			}
			//This checks to see if the current clicked thumb is the last
			else {
				//alert("Im Last " + activeThumb +"");
				$('body').find('#gallerylinks a:first').trigger('click');
			}
			
			return false;
	
	});
		
	// Previous < Arrow
	$("#gallery-previous-image").live('click', function() {
			
			//Define the veriable activeThumb for use with the arrows (+1 to start at 1 and prev = 0)
			var activeThumb = $('.active-thumb').index('#gallerylinks a') - 1;
			//alert("Arrow " + activeThumb + "");
			
			// Get Number of list items
			var numThumb= $('#gallerylinks a').length;
			
			//This checks to see if the current clicked thumb is the first
			if(activeThumb == 1) {
				//alert("Im First " + activeThumb + "");
				 $("body").find('#gallerylinks a').eq(activeThumb).trigger('click');	
			}
			//This checks to see if the current clicked thumb neither the first or last
			else if (activeThumb < numThumb){
				//alert("Im in the middle " + activeThumb + "");
				$('body').find('#gallerylinks a').eq(activeThumb).trigger('click');
			}
			//This checks to see if the current clicked thumb is the last
			else {
				//alert("Im Last " + activeThumb +"");
				$('body').find('#gallerylinks a:first').trigger('click');
			}
			
			return false;
	
	});


	// Click on the thumb nails on the right and the main image will change.
	//Function to rotate Gallery
	// Click Thumbnail	
	$(".image").live('click', function() {
		
		// Momentarily strip all active classes then add it the current clicked item.
		$(".image").removeClass("active-thumb");
		$(this).addClass("active-thumb");
		
		//Define the veriable activeThumb for use with the arrows (+1 to start at 1 and prev = 0)
		var activeThumb = $('#gallerylinks').children('a').index(this) + 1;
		
		// Triggers the changing of the active Image
		// Get the attribute of rel and it becomes "image"
		var image = $(this).attr("rel");
		$('#scroll-content').hide();
		// Replace "image"
		$('#scroll-content').html('<img src="' + image + '" border="0"/>');
		$('#scroll-content').fadeIn('slow');
		
		// Trigger for scroll bar + Reset's the position back to top.
		$('#scroll-content').css({top:0});
		
		// Again delaying the setSlider until image .bind('load')  is complete.
		$('#scroll-content img').bind('load', function() {
			setSlider('#scroll-pane');
		});
		
		// Triggers the changing of the active thumbnail
		$(".image").fadeTo('fast', 0.3);
		$(this).fadeTo('fast', 1 );
		
		return false;
		
	});
	
	
});


//Portfolio Gallery Homepage
$(document).ready(function() {
  $("#carousel").featureCarousel({
 		containerWidth: 700,
 		trackerSummation: false,
 		trackerIndividual: false,
 		//Set's the size
 		largeFeatureWidth: 221,
 		largeFeatureHeight: 235,
 		smallFeatureWidth: 156,
 		smallFeatureHeight: 165 
  });
  setSlider('#scroll-pane');
});


/*$(document).ready(function() {
    $("#carousel-container").load("/usercontent/homepage/carousel.html");
    $("#carousel-container").css('z-index', 17900);
   
});

$('#carousel').live('load', function () {
    $("#carousel").featureCarousel({
        containerWidth: 700,
        trackerSummation: false,
        trackerIndividual: false,
        //Set's the size
        largeFeatureWidth: 221,
        largeFeatureHeight: 235,
        smallFeatureWidth: 156,
        smallFeatureHeight: 165
    });
		setSlider('#scroll-pane');
}); */

function setSlider($scrollpane){
	//change the main div to overflow-hidden so we can use the slider now
	$('#scroll-pane').css('overflow','hidden');
		
	//compare the height of the scroll content to the scroll pane to see if we need a scrollbar
	var difference = $('#scroll-content').height()-552//eg it's 200px longer 
		
	//alert(difference);
	
	// Removes the scroller incase it doesn't need it in next step.
	$("#scrollbox").html('');
	
	if(difference>0)//if the scrollbar is needed, set it up...
	{
	   var proportion = difference / $('#scroll-content').height();//eg 200px/500px
	   var handleHeight = Math.round((1-proportion)*$('#scroll-pane').height());//set the proportional height - round it to make sure everything adds up correctly later on
	   handleHeight -= handleHeight%2; 
	
	   $("#scrollbox").html('<\div id="slider-wrap"><\div id="slider-vertical"><\/div><\/div>');//append the necessary divs so they're only there if needed
	   $("#slider-wrap").height($("#scroll-pane").height());//set the height of the slider bar to that of the scroll pane
	   
	
	   //set up the slider 
	   $('#slider-vertical').slider({
	      orientation: 'vertical',
	      min: 0,
	      max: 100,
	      value: 100,
	      slide: function(event, ui) {//used so the content scrolls when the slider is dragged
	         var topValue = -((100-ui.value)*difference/100);
	         $('#scroll-content').css({top:topValue});//move the top up (negative value) by the percentage the slider has been moved times the difference in height
	      },
	      change: function(event, ui) {//used so the content scrolls when the slider is changed by a click outside the handle or by the mousewheel
	         var topValue = -((100-ui.value)*difference/100);
	         $('#scroll-content').css({top:topValue});//move the top up (negative value) by the percentage the slider has been moved times the difference in height
	      }
	   });
	
	   //set the handle height and bottom margin so the middle of the handle is in line with the slider
	   $(".ui-slider-handle").css({height:handleHeight,'margin-bottom':-0.5*handleHeight});
		
	   var origSliderHeight = $("#slider-vertical").height();//read the original slider height
	   var sliderHeight = origSliderHeight - handleHeight ;//the height through which the handle can move needs to be the original height minus the handle height
	   var sliderMargin =  (origSliderHeight - sliderHeight)*0.5;//so the slider needs to have both top and bottom margins equal to half the difference
	   $(".ui-slider").css({height:sliderHeight,'margin-top':sliderMargin});//set the slider height and margins
	   
	}//end if
	
	//code to handle clicks outside the slider handle
	$(".ui-slider").click(function(event){//stop any clicks on the slider propagating through to the code below
	   	event.stopPropagation();
	   });
	   
	$("#slider-wrap").click(function(event){//clicks on the wrap outside the slider range
		  var offsetTop = $(this).offset().top;//read the offset of the scroll pane
		  var clickValue = (event.pageY-offsetTop)*100/$(this).height();//find the click point, subtract the offset, and calculate percentage of the slider clicked
		  $("#slider-vertical").slider("value", 100-clickValue);//set the new value of the slider
	});
	
	//additional code for mousewheel
	$("#scroll-pane,#slider-wrap").mousewheel(function(event, delta){
		var speed = 5;
		var sliderVal = $("#slider-vertical").slider("value");//read current value of the slider
		
		sliderVal += (delta*speed);//increment the current value
	
		$("#slider-vertical").slider("value", sliderVal);//and set the new value of the slider
		
		event.preventDefault();//stop any default behaviour
	});
	
	 

}












$(window).load(function () {
    ResizeContainer();
});

$(document).ready(function() {
    var nav = window.location.pathname;
    $('ul[id^="ni"] li[^="link"] a[href$="' + nav + '"] ').addClass('activeLink');
});

function SetActiveLink(id)
{
    $('#' + id + ' a').addClass('activeLink');
}

function ResizeContainer()
{  
    var difference = 0;
    
    $('div[id^="mod"]').each(function()
    {
        var currentDifference = parseInt($(this).height() - $(this).css('min-height').replace('px', ''));
        
        if(currentDifference > difference)
        {
            difference = currentDifference;
        }
    });
    
    $('#container').height($('#container').height() + difference);
}

function ResizeModule(name, type)
{
    if(type == 'Form')
    {
        if (typeof(Page_Validators) != 'undefined') 
        {
            if(!Page_IsValid)
            {
                if(!$('.' + name).parent().attr('style'))
                {
                    var offset = 10; 
                    var numberOfValidators = $('span[id*="RequiredFieldValidator"]').length;
                
                    $('.' + name).parent().height($('.' + name).parent().height() + (offset * numberOfValidators));
                    ResizeContainer();
                }
            }
        }
   }
   else
   {
       var spaceBetweenContainer = 10;
       var newHeight = parseInt($(name).height()) + parseInt($(name).css('top').replace('px', '')) + spaceBetweenContainer;
       $('#container').height(newHeight);
   }
}

function ClearInputText(selector)
{
    $(selector).val('');
}

function SetCursorToEndOfText(textControlID)
{
    var text = document.getElementById(textControlID);
    
    if (text != null && text.value.length > 0)
    {
        if (text.createTextRange)
        {
            var fieldRange = text.createTextRange();
            fieldRange.moveStart('character', text.value.length);
            fieldRange.collapse();
            fieldRange.select();
        }
    }
}

String.prototype.endsWith = function(a){ return this.substr(this.length - a.length) === a; }
String.prototype.startsWith = function(a){ return this.substr(0, a.length) === a; }
String.prototype.trim = function(){ return this.replace(/^\s*/, '').replace(/\s*$/, ''); }
String.prototype.trimEnd = function(){ return this.replace(/\s+$/, ""); }
String.prototype.trimStart = function(){ return this.replace(/^\s+/, ""); }

function ShowFullImage(imageSelector)
{
    $(imageSelector).fadeIn(500);
}

function HideFullImage(imageSelector)
{
    $(imageSelector).hide();
}

jQuery(function()  
{  
    $('li.top-nav-links').live('mouseover', function()  
    {  
        if (!$(this).data('init'))  
        {  
            $(this).data('init', true);  
            $(this).hoverIntent  
            (  
                function()  
                {  
                    $(this).find('a.top-nav-link-hover').addClass("top-nav-hover");
                    $(this).find('div.sub').slideDown(100);
                    $(this).css('z-index', 9000 );
                     return false; 
                },  
  
                function()  
                {  
                    $(this).find('div.sub').slideUp(50, function() {
                       $(this).siblings('a.top-nav-hover').removeClass("top-nav-hover");
                       });
                     $(this).css('z-index', 8000 );
                     return false; 
                }  
            );  
            $(this).trigger('mouseover');  
        }  
    });  
});


//Check Boxes on Forms
$(document).ready(function() {
	var name = document.body.id;
	
	if(name == "ctl00_ContactUs")
	{
		document.getElementById("ctl00_cphMain_cbForm229Newsletter").checked=true;
	}
	else if (name == "ctl00_RequestAQuote")
	{
		document.getElementById("ctl00_cphMain_cbForm230Newsletter").checked=true;
	}	

});




