var jQ = jQuery.noConflict();
				  
function staleFlashVersion() {
	var r=confirm("Please upgrade to the new version of Flash Player to enjoy all the content on this site. Clicking OK will open a new window where you can download and install Flash Player.");
	if (r==true) {
		window.open('http://www.macromedia.com/go/getflashplayer', '_blank', '','');
	} else {
		
	}
}
/***
 * Stupid IE conflicts with the slideshow object and makes the height of the shopping
 * cart all wonky. We gotta set a static pixel height instead of a percentage
 */
jQ(function(){ 
	if(jQ('#cart_panel').is(':visible')) {
		if(jQ.browser.msie && parseInt(jQ.browser.version) < 8)
		{
			var height = jQ('#cart_panel').height();
			jQ('#cart_panel').css('height', height+'px');
		}
	}
});
var photoGalleriesReady = true;
/***
 * Function for loading Nav in IE after the onload
 */
function IEonLoadNavs(){
	if (jQ.browser.msie) {
		for (var i in ienavs) {
			ienavs[i](ievars[i].objectId, ievars[i].navId, ievars[i].navWidth, ievars[i].navHeight, ievars[i].hash);
		}
	}
}
/***
 * Create video popup for SWF adviewer
 * @param {Object} videofile
 */
function createAdPopup(videofile) {
	if(videofile!='') {
		if (jQ('#videoPopup').length > 0) {
			jQ('#videoPopup').remove();
		}
		var width = document.documentElement.clientWidth;
		var height = jQ(window).height();
		left = (width/2)-175;
		top = (height/2)-160;
		var popup = jQ('#videoPopup'); // selector cache
		jQ('body').append('<div id="videoPopup" style="display:none;"></div>');
		popup.append('<div id="video_holder"></div>')
			.css('display', 'block');
		jQ('<img src="/RESOURCES/IMG/close_info.png" onclick="jQ(this).parent().remove();" style="cursor:pointer;">').appendTo('#videoPopup');
		jQ("#video_holder").css('margin', '5px 5px 5px 5px');
		popup.css('width', '353px')
			.css('height', '319px')
			.css('backgroundColor', '#b5b2b2')
			.css('position', 'absolute')
			.css('left', left)
			.css('top', top)
			.css('z-index', '100000')
			.css('padding', '10px 10px 10px 10px')
			.draggable();
		
		var params = {};
		params.wmode="transparent";
		params.PLAY = "true";
		var flashvars = {};
		flashvars.videosource = videofile;
		var attributes = {};
		attributes.movie="/RESOURCES/FLASH/simpleVideo.swf";
		attributes.src="/RESOURCES/FLASH/simpleVideo.swf";
		attributes.quality="high";
		swfobject.embedSWF("/RESOURCES/FLASH/simpleVideo.swf", "video_holder", "343", "269", "9.0.0", "/RESOURCES/FLASH/photo_editor/expressInstall.swf", flashvars, params, attributes);
	}
}
/***
 * Apply IE png Fix
 */
function behaviorFix() {
	if (jQ.browser.msie && parseInt(jQ.browser.version) < 7) {
		jQ('#backgroundDiv').contents().find('img').each(function(){ /* <IMG> themselves need height and width for PNG fix to work property, or else the image stretches in IE6 */
			var pheight=jQ(this).parent('div').css('height');
			var pwidth=jQ(this).parent('div').css('width');
			if (typeof(pwidth) != 'undefined' && typeof(pheight) != 'undefined') {
				jQ(this)
					.css('height', pheight)
					.css('width', pwidth);
			}	
		});
	}
	jQ('div').each(function(){
		jQ(this).css('behavior', 'url(/resources/iepngfix.htc)');
	});
	jQ('img').each(function(){
		jQ(this).css('behavior', 'url(/resources/iepngfix.htc)');
	});
}
/***
 * Function that flash calls when the xml has loaded into the nav
 * @param {Object} elem ID
 * @param {Object} width
 * @param {Object} height
 */
function autoSizeNav(elem, width, height) {

	var windowloc = window.location+'';
	if (windowloc.search('fdsafdsa') != -1) {
		jQ('body').append('<div style="height:100px;width:200px;background-color:#f00;text-align:center;line-height:100px;float:left;margin-top:100px;">'+elem+'</div>');
	}
	//alert(width);
	var width = parseInt(width);
	if (jQ('#'+elem).parent().attr('id') == 'object_id_247') { // hack for nav cuz jojo isn't around
		width = parseInt(width)+250;
	}	
	var height = parseInt(height);
	var location = window.location+'';
	if (location.search(/snoop/i) != -1 && jQ.browser.msie) { //IE hack for snoop demo
		var flashEmbed = jQ('object[movie=/RESOURCES/FLASH/standard_nav_182.swf]').get(0);
	} else {
		var flashEmbed = document.getElementById(elem);
	}
	jQ('#'+elem).attr('height', height);
	jQ('#'+elem).attr('width', width);
}

/***
 * Document ready, don't cach background images of divs
 */
jQ(function(){
	bannerZindex();
	if(jQ.browser.msie) {
		try {
			document.execCommand("BackgroundImageCache", false, true);
		} catch(err) {
			
		}
	}
	jQ('.imageText').each(function() {
		jQ(this).hide();
	});
});
/***
 * Try to figure out what the highest Standard Object of the page is and apply it to IBanner
 */
function bannerZindex() {
	if (jQ('.bg_banner_tb').length > 0 || jQ('.bg_banner_sb').length > 0) {
		var h_index = 0;
		//jQ("div:not(.page_object)").each(function(){
		jQ("div").each(function(){	
			if (parseInt(jQ(this).css('z-index')) > h_index) {
				h_index = parseInt(jQ(this).css('z-index'));
			}
		});
		jQ('.bg_banner_tb, .bg_banner_sb')
			.css('z-index', h_index);
	}
}
/***
 * Thumbalize Image Function
 * @param {Object} target
 * @param {Object} image
 */
function thumbalizeMe(target, image){
	jQ('#'+target).click(function() {
		var loading = 0;
		jQ('#thumbalizeLightBox').remove();
		jQ('#ttt_loadingLightBox').remove();
		jQ('body').append('<div id="ttt_loadingLightBox">Loading Image</div>');
		center(jQ('#ttt_loadingLightBox'));
		
		jQ('body').append('<div id="thumbalizeLightBox">\
								<div id="ttt_lbwrap">\
									<img id="lbimage" src="'+image+'"/>\
								</div>\
									<div id="ttt_closeLightBox">close</div>\
									<div id="ttt_lightBoxInfo">Loading...</div>\
								</div>');
								
		jQ('#ttt_closeLightBox').click(function() {
			jQ('#thumbalizeLightBox').remove();
		});
		
		if (!jQ.browser.msie) {
			jQ('#lbimage').unbind('load').load(function(){
				loading++;
				if (loading == 2) {
					showLightBox();
				}
			});
		}
		
		//compare image and info heights, adjust where necessary
		if (jQ('#ttt_lightBoxInfo').height() > jQ('#lbimage').height() && jQ('#ttt_lightBoxInfo').height() <= 400) { //info is larger than image, it will overflow out of the lightbox so adjust lightbox height
			jQ('#thumbalizeLightBox').height(jQ('#ttt_lightBoxInfo').outerHeight() + 20);
		} else if (jQ('#ttt_lightBoxInfo').height() > jQ('#lbimage').height() && jQ('#ttt_lightBoxInfo').height() > 400) { //info is larger than image and larger than 500px, constrain it at 500, add scrollbar and adjust the height of the lightbox
			jQ('#ttt_lightBoxInfo').css('overflow', 'auto').height(500);
			jQ('#thumbalizeLightBox').height(jQ('#ttt_lightBoxInfo').outerHeight() + 20);
		}
		
		//ajax call to get title and description
		ONAPI.request({
			data: {
				module: 'photos',
				action: 'get',
				id: this.id.split('_')[1]
			},
			callback: function(data){
				if (data[0]) {
					data = data[0];
					if (data['name']) {
						var name = data['name'];
					} else {
						var name = '';
					}
					if (data['description']) {
						var description = data['description'];
					} else {
						var description = '';
					}
					if (!name && !description) {
						jQ('#ttt_lightBoxInfo').remove();
						jQ('#ttt_lbwrap').css('margin-right', '0px').css('margin-top', '20px');
					} else {
						jQ('#ttt_lightBoxInfo').html('<b>' + name + '</b><p>' + description + '</p>');
					}
				} else {
					jQ('#ttt_lightBoxInfo').remove();
				}
				if (jQ.browser.msie) {
					showLightBox();
				} else {
					loading++;
					if (loading == 2) {
						showLightBox();
					}
				}
			},
			error: function() {
				jQ('#ttt_lightBoxInfo').remove();
				jQ('#ttt_lbwrap').css('margin-right', '0px').css('margin-top', '20px');
				if (jQ.browser.msie) {
					showLightBox();
				} else {
					loading++;
					if (loading == 2) {
						showLightBox();
					}
				}
			}
		});
		
	});
}

function showLightBox(){
	center(jQ('#thumbalizeLightBox'));
	jQ('#ttt_loadingLightBox').fadeOut(200, function() {jQ('#ttt_loadingLightBox').remove();});
	jQ('#thumbalizeLightBox').fadeIn(300);
}

function center(object) {
	width = jQ(object).width();	
	height = jQ(object).height();	
	containerWidth = jQ(window).width();
	containerHeight = jQ(window).height();
	
	width = width/2;
	height = height/2;
	containerWidth = containerWidth/2;
	containerHeight = (containerHeight/2)+jQ(window).scrollTop();
	
	leftOffset = containerWidth - width;
	topOffset = containerHeight - height;
	if (topOffset < 20) {
		topOffset = 20;
	}
	
	jQ(object).css('left', leftOffset+'px').css('top', topOffset+'px');
}
