
function PopUp(URL) {
	
	//day = new Date();
	//id = day.getTime();
	
	//eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=950,height=670,left = 100,top = 100');");
	window.location.href=URL;
}


function StarckPopUp(URL) {
	
	day = new Date();
	id = day.getTime();
	
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=590,height=395,left = 100,top = 100');");

}

$(document).ready(function(){
	
	// code to handle image transitions (along with ad refreshes & omniture stats updating)
	
	/*
	This code references a number of global vars, for flexibility
	These are set server-side at the template level
	See gallery_js.tpl
	currentImage, adtechKey, imgContainerDiv, navLinksDiv, captionElem, adDivs, slideshowLinkElem, 
	*/
	
	// if IE 6, give up now...
	isIE6=(navigator.userAgent.toLowerCase().indexOf('msie 6') != -1) && (navigator.userAgent.toLowerCase().indexOf('msie 7') == -1);
	
	if (isIE6) {
		return;
	}
	
	// how many images do we have? Get this here for use in various loops below
	var arrayLength = imagesArray.length;
	
	// bookmarked image - will be numeric ID in location hash
	if ((parent.location.hash != '') && (parent.location.hash.substring(1) == parseInt(parent.location.hash.substring(1)))) {
		currentImageID=parseInt(parent.location.hash.substring(1));
		for (var i=0;i<arrayLength;i++) {
			if (parseInt(imagesArray[i]['image_group_id']) === currentImageID) {
				currentImage = i;
				break;
			}
		}
		if ((currentImage) && (imagesArray[currentImage])) {
			var myElem = $('div#'+imgContainerDiv).children('a').children('img:first-child');
			if (myElem) {
				myElem.attr('src',imagesArray[currentImage]['filename']);
			
				// now change the displayed image counter
				var myListElem = $('ul#'+navLinksDiv).children('li:first-child');
				if (myListElem) {
					myListElem.html((currentImage+1) + ' of ' + arrayLength);
				}
				
				// now swap out the caption
				/*
				var myCaptionElem = $('p#'+captionElem);
				if (myCaptionElem) {
					myCaptionElem.html(unescape(imagesArray[currentImage]['image_caption']));
				}
				*/
				// pjc 2010-05-27
				// fix for FCK-mangled HTML adding extra p tags - remove all p elems from that div
				$('div#imageCaption').remove();
				$(document.createElement('div')).attr('id','imageCaption').html(unescape(imagesArray[currentImage]['image_caption'])).prependTo($('div#captionArea'));
			}
		}
	}
	
	var block = false; // timeout, prevent clicking through too rapidly
	
	// add event handlers to the back and forward links
	$('a.prev, a.next').click(function(event){

		if (block) {
			return false;
		}
		
		// load ads first
		
		var adDivsLength=adDivs.length;
		
		$('div#eyeDiv').remove(); //hard-coded for an Audi ad campaign that wrote a div outside of it's container
		
		// now update the ad divs/iframes
                // Updated the code so that instead of removing the topbanner div with the iframe, the iframe is inserted into the topbanner div
                // This fixed the issue with Amex on the gallery section
		for (var i=0;i<adDivsLength;i++) {
			var myDiv = adDivs[i];
				if ($('#'+myDiv).is('div')) { // first call after page load, so convert div to iframe
				var myIFrame;
				if (document.createElement && (myIFrame = document.createElement('iframe'))) {
					// set correct display/css properties
					// clone some of the div css properties
					$(myIFrame).css({border : '0', 'zIndex': 999, overflow : 'visible', 'width' : $('div#'+myDiv).width(), 'height' : $('div#'+myDiv).height()} );
					
					// set HTML attributes
					$(myIFrame).attr({frameborder : '0', scrolling : 'no', allowtransparency : 'true' });
					$('div#'+myDiv).children().each(function (index) { $(this).remove(); });
					//$('div#'+myDiv).replaceWith($(myIFrame)).remove();
					$(myIFrame).attr({src : '/content/adtech/'+myDiv+'.html', id : myDiv })
                    $('div#'+myDiv).html($(myIFrame));
				}
			} else if ($('#'+myDiv).is('iframe')) { // subsequent call after page load, so already converted to iframe
				document.getElementById(myDiv).contentWindow.location.reload(true);
			}
		}
		
		// now post meta data to omniture
		if (typeof s != "undefined") {
			void(s.t());
		}
		
		if(typeof(_gaq) != "undefined") {
		        _gaq.push(
		          ['localPageTracker0._setAccount', 'UA-67691-2'],
		          ['localPageTracker0._trackPageLoadTime'],
		          ['localPageTracker0._trackPageview', location.host+location.pathname]
		        );

		        _gaq.push(
		          ['globalPageTracker0._setAccount', 'UA-11428852-1'],
		          ['globalPageTracker0._trackPageLoadTime'],
		          ['globalPageTracker0._trackPageview', location.host+location.pathname]
		        );
		}
		
		// now load new image
		
		// in which direction are we going?
		if ($(this).hasClass('next')) {
			currentImage++;
			if (currentImage == arrayLength) { // loop back to start
				currentImage = 0;
			}
		} else if ($(this).hasClass('prev')) {
			currentImage--;
			if (currentImage < 0) { // loop back to end
				currentImage = (arrayLength - 1);
			}
		}
		
		var myElem = $('div#'+imgContainerDiv).children('a').children('img:first-child');
		if (myElem) {
			myElem.attr('src',imagesArray[currentImage]['filename']);
			
			// now change the displayed image counter
			var myListElem = $('ul#'+navLinksDiv).children('li:first-child');
			if (myListElem) {
				var imageNumber=currentImage+1;
				if (parseInt(imageNumber)<10){ // add leading zero
					imageNumber=""+"0"+imageNumber;
				}
				var imageTotal = arrayLength;
				if (parseInt(imageTotal)<10){ // add leading zero
					imageTotal=""+"0"+imageTotal;
				}
				myListElem.html(imageNumber + ' of ' + imageTotal);
			}
			
			// now swap out the caption
			/*
			var myCaptionElem = $('p#'+captionElem);
			if (myCaptionElem) {
				myCaptionElem.html(unescape(imagesArray[currentImage]['image_caption']));
			}
			*/
			// fix for FCK-mangled HTML adding extra p tags - remove all p elems from that div
			// pjc 2010-06-18 changed to div - captions formatted with <p> tags screws up display
			$('div#imageCaption').remove();
			$(document.createElement('div')).attr('id','imageCaption').html(unescape(imagesArray[currentImage]['image_caption'])).prependTo($('div#captionArea'));
		} else {
			return;
		}
		
		// add to hash for bookmarking purposes
		if (imagesArray[currentImage]['image_group_id']) {
			parent.location.hash = imagesArray[currentImage]['image_group_id'];
		}
		
		delay = 0; // time in milliseconds of delay between clicks
		
		if (delay > 0) {
			block = true; // put a block on rapid click-throughs
			window.setTimeout(function() {
				block = false;
			}, delay);
		}
		
		return false;  // prevent the href action of the links
	});

	// variable used in slideshow
	var myInterval = null;

	$('a#'+slideshowLinkElem).click(function(event){
		// function to start/stop the gallery slideshow, & update the link text accordingly
		var htmlStr = $(this).html().trim().toLowerCase(); // sanitise link text for string comparison
		if (htmlStr=='start slideshow') 
		{ // kick it off
			$(this).html('Stop Slideshow'); // change link text
			myInterval = setInterval (slideshow, 4500 );
		}
		else
		{ // kill it
			clearInterval(myInterval);
			myInterval = null;
			$(this).html('Start Slideshow'); // change link text
		}
		return false;  // prevent the href action of the links
	});
	
	function slideshow () 
	{ // raise the click event of the Next link
		$('a.next:first').click();
	}
	
	// calculate the currently displayed image
	var currentImageFileName=$('div#'+imgContainerDiv).children('a').children('img').attr('src');
	if (currentImageFileName) {
		for (var i=0;i<arrayLength;i++) {
			if (imagesArray[i]['filename'] == currentImageFileName) {
				currentImage = i;
				break;
			}
		}
	}
	// do some image preloading to speed things up
	if(arrayLength < 50)
	{
		for (var i=0;i<arrayLength;i++) {
		eval ("img" + i + "= new Image();");
		eval ("img" + i + ".src = imagesArray[i]['filename'];");
		}
	}

	
});

