/* stop IE6 flicker */
try { document.execCommand("BackgroundImageCache", false, true); } catch(err) {}

// variables for various things
var mouseOvers = new Array();
var mouseOuts = new Array();

var currentImage = -1;

/* on dom loaded (jquery) */
$(document).ready(function(){
	
	// add wrapper divs to make shadows (except for IE 6)
	if (!$.IE6Below()) {
		$("#header").wrap("<div id='header-wrapper'></div>");
		$("#header-wrapper").append("<div id='top-shadow'></div><div id='bottom-shadow'></div>");
		$("#main").wrap("<div id='main-wrapper'></div>");
		$("#main-wrapper").append("<div id='top-shadow'></div><div id='bottom-shadow'></div>");
		$("#footer").wrap("<div id='footer-wrapper'></div>");
		$("#footer-wrapper").append("<div id='top-shadow'></div><div id='bottom-shadow'></div>");
	};
	
	// wrap any 'framed' images
	$("img.framed").wrap("<div class='frame-wrapper'></div>");
	
	// increase font size on select box field for safari 2
	if ($.Safari2Below()) {
		$("#search-location").addClass("larger");
		$("#newsletter-sign-up, #search-submit").addClass("different");
		$(".search-events-box").addClass("search-events-box-smaller");
	};
	
	// increase size on select box field for safari 3
	if ($.browser.safari) {
		if (!$.Safari2Below()) {
			$("#search-location").addClass("bugfix");
		};
	};


	// in safari 2 make sure feature-description-box stays at bottom of feature-box
	// even when the feature-box resizes due to image height being unknown and image not loaded
	if ($.browser.safari) {
		if ($.Safari2Below()) {
			$.fbInitialized = false;			
			$.safariResizeFix = function() {
				if (!$.fbInitialized) {
					$.fbInitialized = true;
					$.fbImage = $(".feature-image")[0];
					setTimeout("$.safariResizeFix()", 50);
					return;
				};
				if ($.fbImage.complete || $.fbImage.height > 0) {
					$(".feature-description-box").css("bottom","-1px");
				} else {
					setTimeout("$.safariResizeFix()", 50);
				};
			};
			if ($(".feature-image").size()>0) $.safariResizeFix();
		};
	};

		// add default text functionality to input fields
	$('.input-default-text').each(function(i) {
		var defaultValue = $(this).attr('value');
		$(this).focus(function () { if (this.value == defaultValue) this.value = ''; this.style.color = '#333333' });
		$(this).blur(function () { if (this.value == '') { this.value = defaultValue; this.style.color = '#666666' }});
	});
	
	// preload some images we use for rollovers on anything with the "rollover" class
	// this will find an image such as something.png and preload something-o.png
	// this should work with any extension, .png .gif .jpg .jpeg etc
	$(".rollover").each(function(i){
			var imageURL = $(this).attr("src"); // e.g. "http://foo/bla.png"
			var cleanImageURL = imageURL.substring(0, imageURL.length); // e.g. "http://foo/bla.png"
			var imageSuffix = cleanImageURL.substring(cleanImageURL.lastIndexOf('.'), cleanImageURL.length); // e.g. ".png"
			var lastTwoChars = cleanImageURL.substring(cleanImageURL.length-imageSuffix.length-2,cleanImageURL.length-imageSuffix.length);// e.g. "-o"
			if (lastTwoChars!='-o') {
				var newImageURL = cleanImageURL.substring(0,cleanImageURL.length-imageSuffix.length) + "-o" + imageSuffix;
				mouseOvers[i] = new Image();
				mouseOvers[i].src = newImageURL;
				mouseOuts[i] = new Image();
				mouseOuts[i].src = cleanImageURL;
			};
			$(this).mouseover(function() {
				this.src = mouseOvers[i].src;
			});
			$(this).mouseout(function() {
				this.src = mouseOuts[i].src;
			});
	});
	
});


/**************** other misc functions *****************/

// Return true if browser is Internet Explorer and version 6 or earlier
$.IE6Below = function() {
	if ($.browser.msie) {
		try {
			if (ScriptEngineMajorVersion() <= 5 && ScriptEngineMinorVersion() < 7) { // ie 6- only
				return true;
			}
		} catch(err) { return false; };
		return false;
	} else {
		return false;
	};
};

// Return true if browser is Safari 2.0 or earlier
$.Safari2Below = function() {
	if ($.browser.safari) {
		var kitName = "applewebkit/";
		var tempStr = navigator.userAgent.toLowerCase();
		var pos		  = tempStr.indexOf(kitName);
		var isAppleWebkit = (pos != -1);
		var kitVersion = 0		
		var kitVersion = tempStr.substring(pos + kitName.length,tempStr.length);
				kitVersion = kitVersion.substring(0,kitVersion.indexOf(" "));
				kitVersion = kitVersion.substring(0,3);
		return (kitVersion < 420) ? true : false;
	};
};


function app_init() {
  //image_area = document.getElementById('imagearea');
  image_div = document.getElementById('image');
  form_tag = document.getElementById('purchaseimageform');
  vwitem = document.getElementById('vwitem');
  //title_div = document.getElementById('titlebar');
  //thumb_div = document.getElementById('thumbs');
  //ui_vis('opts', 1, 1);
  //ui_vis('slide_fwd', 1, 1);

  //document.onkeypress = app_onkeypress;
  //if (window.attachEvent) window.attachEvent('onresize', app_onresize);
  //else if (window.addEventListener) window.addEventListener('resize', app_onresize, false);

  //if (app_is_ie) {
    //image_area.style.width = title_div.style.width = '100%';
    //thumb_div.style.overflow = 'scroll';
    //document.body.parentNode.style.overflow = 'hidden';
    //document.onkeydown = app_onkeydown;
  //} else if (app_is_opera) {
    //document.getElementById('options').style.width = '180px';
  //}

  //app_getwinsize();
  //app_getcookie();
  //if (data_count > 0) image_show(data_view);
}

function nextImage()
{
	store_data = document.getElementById('store_data_' + currentImage);
	$nextImageId = store_data.getAttribute('nextImageId');
	image_show($nextImageId);
}

function previousImage()
{
	store_data = document.getElementById('store_data_' + currentImage);
	$previousImageId = store_data.getAttribute('previousImageId');
	image_show($previousImageId);
}

function image_show(i) {
  //slide_reset(); 
  image_index = i;
  currentImage = i;
  ui_sethtml('title', document.getElementById('title_'+image_index).innerHTML);
  //image_setsize();
  //if (options_on) options_setsize();
  //if (data_iw[i] < 0) {
    //image_div.innerHTML = '<iframe style="width:100%;height:' + (image_area.offsetHeight-4)
      //+ 'px" frameborder="0" src="' + document.getElementById('img_'+i).href + '"></iframe>';
    //ui_vis('fit_size', 0); ui_vis('full_size', 0);
  //} else {
    //var s = image_fit(1);
    image_div.innerHTML = '<img name="slide" src="' + document.getElementById('img_'+i).href
      + '" ' + ' alt=""/>' +
	'<a href="#"  onclick="closeLayer(\'image\'); closeLayer(\'title\'); closeLayer(\'options\'); return false"><img class="close rollover" src="/images/layout/buttons/close.png" width="46" height="17" alt="Close" /></a>';
  //}
  //image_setbuttons();
  //if (slide_inprog && !slide_on) slide_inprog = 0;

  // change form_tag.action and form_tag.vwitem
  store_data = document.getElementById('store_data_' + image_index);
  form_tag.action = store_data.getAttribute('storeURL');
  vwitem.value = store_data.getAttribute('storeItemId');
  openLayer('image');
  openLayer('title');
  openLayer('options');
} 

function ui_sethtml(id,html) {
  document.getElementById(id).innerHTML = html;
}

function openLayer( whichLayer )
{
	var elem, vis;
	if( document.getElementById ) // this is the way the standards work
		elem = document.getElementById( whichLayer );
	else if( document.all ) // this is the way old msie versions work
		elem = document.all[whichLayer];
	else if( document.layers ) // this is the way nn4 works
		elem = document.layers[whichLayer];
	vis = elem.style;
	vis.display = 'block';
}
function closeLayer( whichLayer )
{
	var elem, vis;
	if( document.getElementById ) // this is the way the standards work
		elem = document.getElementById( whichLayer );
	else if( document.all ) // this is the way old msie versions work
		elem = document.all[whichLayer];
	else if( document.layers ) // this is the way nn4 works
		elem = document.layers[whichLayer];
	vis = elem.style;
	vis.display = 'none';
}

