
$(document).ready(function () {
	
	//handleContactForm();
	switch (getUrlVarsFrom(window.location.href, true)['tabid']) {
		case undefined:
			initSlideshow();
			break;
		case '27':
			initSlideshow();
			break;
		case '24':
			handleCatalogueBoxes();
			break;
		
		}
		
		highlightCurrent();
	handleHeaders();
    handlePlaceHolderText();
}) 


var t;

function slideSwitch() {
    var $active = $('div#slideshow div.slides div.active');

    if ( $active.length == 0 ) $active = $('div#slideshow div.slides div:last');

    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('div#slideshow div.slides div: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}, 1500, function() {
		.animate({opacity: 1.0}, 3500, function() {
            $active.removeClass('active last-active');
			if (t) clearTimeout(t);
			t=setTimeout( "slideSwitch()", 3500 );
        });
}




function initSlideshow() {
	$('div#slideshow div.slides img').each(function(index, domEle) {
		
		$(domEle).attr('src', $(domEle).attr('src').replace(/cell=[^&]/,'cell=842,489'));
		
	})
	$('div#slideshow div.slides div:first').addClass('active');
	t=setTimeout( "slideSwitch()", 3500 );
}




function getUrlVarsFrom(url, preventUnwantedVars) {
    var vars = [],
        hash;
    var hashes = url.slice(url.indexOf('?') + 1).split('&');
    for (var i = 0; i < hashes.length; i++) {
        hash = hashes[i].split('=');
		if (preventUnwantedVars) {
			if (hash[0] != 'lg' && hash[0] != 'objectid' && hash[0] != 'tabindex' && hash[0] != 'page' && hash[0] != 'keyword'  && hash[0] != 'mediaid') {
        		vars.push(hash[0]);
        		vars[hash[0]] = hash[1];
			}
		} else {
			vars.push(hash[0]);
        	vars[hash[0]] = hash[1];
		}
    }
    return vars;
}



function handlePlaceHolderText() {
    $('input[type=text]').focus(function () {
        if ($(this).val() == $(this).attr('defaultValue')) {
            $(this).val('');

        }
    });

    $('input[type=text]').blur(function () {
        if ($(this).val() == '') {
            $(this).val($(this).attr('defaultValue'));

        }
    });
	
	
	 $('textarea').focus(function () {
        if ($(this).val() == $(this).attr('defaultValue')) {
            $(this).val('');

        }
    });

    $('textarea').blur(function () {
        if ($(this).val() == '') {
            $(this).val($(this).attr('defaultValue'));

        }
    });
	
}



//MENU FUNCTIONS
function nrKeys(a) {
    var i = 0;
    for (key in a) {
        i++;
    }
    return i;
}
function compareAssociativeArrays(a, b) {
   if (a == b) {
       return true;
   }   
   if (nrKeys(a) != nrKeys(b)) {
       return false;
   }
   for (key in a) {     
     if (a[key] != b[key]) {
         return false;
     }
   }
   return true;
}


function highlightCurrent() {
//check for matches on menu
    $(".mainMenu li a").each(function (index, domEle) {
		
		if (compareAssociativeArrays(getUrlVarsFrom(window.location.href,1),getUrlVarsFrom($(domEle).attr('href'),1))) {

			$(domEle).addClass('active');
		}
		
		
    });

			
}

function handleHeaders() {
	var subMenu = false;
	if (getUrlVarsFrom(window.location.href, false)['categoryid']) {
	switch (parseFloat(getUrlVarsFrom(window.location.href, false)['categoryid'])) {
		case 8676:
		$("#top").addClass('bronze');
		subMenu = true;
		break;
		case 8677:
		$("#top").addClass('marble');
		subMenu = true;
		break;
		case 8678:
		$("#top").addClass('terracotta');
		subMenu = true;
		break;
		case 8679:
		$("#top").addClass('otherMaterials');
		subMenu = true;
		break;
		case 1799:
		$("#top").addClass('centuries19th20th');
		subMenu = true;
		break;
		case 9936:
		$("#top").addClass('centuries19th20th');
		subMenu = true;
		break;
		}
	}
	if(subMenu) {
		
		$('div#top ul.mainMenu li ul').css('display', 'block');
		
		$('div#top ul.mainMenu a.active').parent().parent().parent().children('a').addClass('active');
	}
}
function handleContactForm() {
	if (getUrlVarsFrom(window.location.href, false)['tabid'] == 50) {
		$('.CommandButton').click(function(event) {
			$('input[type=text]').each(function (index, domEle) {
				if ($(domEle).val() == $(domEle).attr('defaultValue')) {
           		$(domEle).val('');

        		}
			})
			$('textarea').each(function (index, domEle) {
				if ($(domEle).val() == $(domEle).attr('defaultValue')) {
           		$(domEle).val('');

        		}
			})

		})
	}
}


function handleCatalogueBoxes() {
	$("div.imageArea a").colorbox({
		width: '900px'
		
	});


}

