//var divAdvertisement;
//var divAdvertDescription;

$(document).ready(function() {

$('#advert_slide').innerfade({ animationtype: 'fade', speed: 2000, timeout: 7000, type: 'sequence', containerheight: '270px' });
	
	/*	
	divAdvertisement = document.getElementById("slideshow");
	divAdvertDescription = document.getElementById("divAdvertDescription");
	
	if (divAdvertisement)
	{
		$("#slideshow a[rel='slide']").crossf4de({
			//callBack: slideshowCallback,
			fadeDuration: 2000,
			pauseDuration: 5000			
		});		
	}
	*/
});


function showMenu(sender)
{
	var mainMenus = $(".divMainMenuItem a");
	var index;
	for (var i = 0; i < mainMenus.length; i++)
	{
		mainMenus[i].className = "";
		if (mainMenus[i] == sender)
		{
			mainMenus[i].className = "selected";
			index = i;
		}
	}
	var subMenus = $(".divHiddenSubMenu");
	if (index != undefined)		
 		document.getElementById("divSubMenu").innerHTML = subMenus[index].innerHTML;
}


var testimonialIndex = 0;
var testimonialList = null;
var attestorList;
function loadTestimonial()
{
 if (testimonialList == null)
 {
 testimonialList = $(".divHideTestimonials .jqTestimonial");
 attestorList = $(".divHideTestimonials .jqAttestor");
 }
 $("#divFadeTestimonials").fadeTo("slow", 0.0, function() {
 if (testimonialIndex >= testimonialList.length)
 {
 testimonialIndex = 0;
 }
 document.getElementById("lblTestimonial").innerHTML = testimonialList[testimonialIndex].innerHTML;
 document.getElementById("lblAttestor").innerHTML = attestorList[testimonialIndex].innerHTML;
 testimonialIndex++;
 $(this).fadeTo("slow", 1.0, function () {setTimeout("loadTestimonial()", TheoTestimonialSeconds);});
 }
 );
}

/* Portfolio */

var SWATCH_WIDTH = 106; //width: 115 + 3 x 2 , height= 55 + 5 x 2
var SWATCH_PER_PAGE = 4; //overriden in gallery.aspx

var currentMargin = 0;
var currentIndex = 0;
function moveLeft()
{
 if (currentIndex <= 0)
 {
 currentIndex = swatches.length - SWATCH_PER_PAGE;
 movePhoto(-(SWATCH_WIDTH * (currentIndex)));
 }
 else
 {
 movePhoto(SWATCH_WIDTH);
 currentIndex--;
 }
}

function moveRight()
{
 if (currentIndex >= (swatches.length - SWATCH_PER_PAGE))
 {
 currentIndex = 0;
 movePhoto(SWATCH_WIDTH * (swatches.length - SWATCH_PER_PAGE));
 }
 else
 {
 movePhoto(-SWATCH_WIDTH);
 currentIndex++;
 }
}

function movePhoto(offset)
{
 currentMargin += offset;
 var val = currentMargin + "px";
 var duration = 1000;
 if ((offset != SWATCH_WIDTH) && (offset != -SWATCH_WIDTH))
 {
 duration = 2500;
 }
 $("#divPortfolioSlide").animate({marginLeft: val}, duration, "swing");
}

var grayscaleParameter = "&grayscale=true";
var currentlyLoading = "";
function colorImage(sender, newWidth)
{
	 $('#imgHomeFeature').stop(true, true);//.fadeIn();
	 $('#imgHomeFeature').fadeOut('slow', function()
	 {
	 sender.src = sender.src.replace(grayscaleParameter, "");
	 var largeImage = new Image();
	 largeImage.onload = function()
	 {		
		 if (currentlyLoading == largeImage.src)
		 {
			 var imageList = $(".divPortfolioGalleryWrapper .imgGallery");
			 var descriptionList = $(".divPortfolioGalleryWrapper .spanText");
			 for (var i = 0; i < imageList.length; i++)
			 {
				 if (imageList[i] == sender)
				 {
					 var imgHomeFeature = document.getElementById("imgHomeFeature");
					 imgHomeFeature.src = largeImage.src;
					 $('#imgHomeFeature').fadeIn('slow');
					 if (i < descriptionList.length)
					 {
					 	document.getElementById("lblPortfolioDetails").innerHTML = descriptionList[i].innerHTML;
					 }
				 }
				 else
				 {
					 if (imageList[i].src.indexOf(grayscaleParameter) < 0)
					 {
					 	imageList[i].src = imageList[i].src + grayscaleParameter;
					 }
				 }
		 	}
		 }
	 }
	 if (isGif(sender.src))
	 {
		 var fileParam = "&fileName=";
		 var param = sender.src.lastIndexOf(fileParam);
		 currentlyLoading = ("http://" + document.domain + sender.src.substr(param + fileParam.length, sender.src.length)).replace(" ", "%20");
	 }
	 else
	 {
	 	currentlyLoading = sender.src.replace("width=98", "width=" + newWidth);
	 
	 }
	 largeImage.src = currentlyLoading;
	 });
}

function isGif(filename)
{
 var dot = filename.lastIndexOf(".");
 if( dot == -1 )
 {
 return false;
 }
 else
 {
 var extension = filename.substr(dot,filename.length).toLowerCase();
 //alert(extension);
 return extension == ".gif";
 }
}

function uncolorImage(sender)
{
 //sender.src = sender.src += grayscaleParameter;
} 

function slideSwitch() {
    var $active = $('#slideshow IMG.active');

    if ( $active.length == 0 ) $active = $('#slideshow IMG:last');

    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow IMG:first');

    // uncomment the 3 lines below to pull the images in random order
    
    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );


    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}

function slideshowCallback(Items, Index)
{
	var link = Items[Index][1];
				
	//divAdvertDescription.innerHTML = advertRecords[Index][1];
	//document.getElementById("divAdvertDescription").innerHTML = advertRecords[Index][1];
	
	/*
	var clickHandler = null;
	var mouseStyle = "";
	if (link)
	{
		clickHandler = function() { document.location = link };
		mouseStyle = "pointer";
	}
	divAdvertisementLeft.onclick = clickHandler;
	divAdvertisementLeft.style.cursor = mouseStyle;	
	*/
}
