function pop_window(url, w, h) {
  $.get(url, function(response) {
    $.fancybox({
  	  autoDimensions: false,
  	  content: response,
  	  width: w,
  	  height: h,
  	  padding: 20
  	});
  });
}

function getObject(objectId) {
	if (document.getElementById && document.getElementById(objectId)) {
		return document.getElementById(objectId);
	} else if (document.all && document.all(objectId)) {
		return document.all(objectId);
	} else {
		return false;
	}
}

function hide_it(id) {
	var myObj = getObject(id);
	if (myObj.style != false) myObj.style.display = 'none';
}

function show_it(id) {
	var myObj = getObject(id);
	if (myObj.style != false) myObj.style.display = 'block';
}

function populate_it(myField, myValue, myArrayName) {
	var selectedArray = eval(myArrayName + myValue);
	while (selectedArray.length < myField.options.length) {
		myField.options[(myField.options.length - 1)] = null;
	}
	for (var i=0; i < selectedArray.length; i++) {
		eval("myField.options[i]=" + "new Option" + selectedArray[i]);
	}
}

function select_it(myField, myValue) {
	var elmLength = myField.length;
	for (var i = 0; i < elmLength; i++) {
		if (myField.options[i].value == myValue) {
			myField.selectedIndex = i
		}
	}
}

function swap_image(imageName, imageUrl) {
	document.images[imageName].src = imageUrl;
}

function bookmark_page() {
	title = document.title;
	url = location.href;
	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,"");
	} else if ( window.external ) { // IE Favorite
		window.external.AddFavorite( url, title);
	} else if(window.opera && window.print) { // Opera Hotlist
		alert("Press CTRL-D (Netscape) or CTRL-T (Opera) to bookmark");
	}
}

function email_page(url) {
	pop_window('/products/email_to_friend.php?url=' + url, 400, 280);
}

function slide_show(index, delay, counter) {
	var slideShowSpeed = delay * 1000; // slideShow speed
	var crossFadeDuration = delay; // duration of crossfade
	var t;
	var imageName = 'myPromo' + index;
	//alert(imageName);
	var container = document.getElementById(imageName);
	if (counter == null) counter = 0;
	var imageArrayName = 'imageArray' + index;
	//alert('var imageArray = ' + imageArrayName + ';');
	eval('var imageArray = ' + imageArrayName + ';');
	var arrLength = imageArray.length;
	var preLoad = new Array();
	for (i = 0; i < arrLength; i++) {
		preLoad[i] = new Image();
		preLoad[i].src = imageArray[i][0];
	}
	if (imageArray[counter][1] != '') {
		var target = '';
		if (imageArray[counter][2] > 0) target = ' target="_blank"';
		container.innerHTML = '<a href="' + imageArray[counter][1] + '"' + target + '><img src="' + preLoad[counter].src + '" id="sub' + imageName +  '" alt="" /></a>';
	} else {
		container.innerHTML = '<img src="' + preLoad[counter].src + '" border="0" id="sub' + imageName + '" alt="" />';
	}
	var subImage = document.getElementById('sub' + imageName);
	if (document.all) {
		subImage.style.filter="blendTrans(duration=2)";
		subImage.style.filter="blendTrans(duration=crossFadeDuration)";
		subImage.filters.blendTrans.Apply();
	}
	if (document.all) {
		subImage.filters.blendTrans.Play();
	}
	counter = counter + 1;
	if (counter >= (arrLength)) counter = 0;
	if (arrLength > 1) t = setTimeout('slide_show('+index+', '+delay+', '+counter+')', slideShowSpeed);
}

function promos(promosArray, promoLocationId) {
	var promoSpeed = 5 * 1000; // speen of rotation
	var promoIndex = 0;
	rotatePromo(promosArray[promoIndex], promoLocationId);
	function rotatePromo(promoDetails, promoLocationId, promoWidth, promoHeight) {
		if ($('#myPromo' + promoLocationId).html() > '') $('#myPromo' + promoLocationId).hide();
		if (!promoDetails[0].match(/\.swf$/i)) { // image
			$('#myPromo' + promoLocationId).html('<img src="' + promoDetails[0] + '" border="0" alt="" />');
			if (promoDetails[1] > '') {
				if (promoDetails[2] > 0) {
					$('#myPromo' + promoLocationId + ' img').wrap('<a href="' + promoDetails[1] + '" target="_blank"></a>');
				} else {
					$('#myPromo' + promoLocationId + ' img').wrap('<a href="' + promoDetails[1] + '"></a>');
				}
			}
		} else { // flash
			var soPromo = new SWFObject(promoDetails[0], 'flashPromo', '568', '140', '8.0.0', 'FFFFFF');
			soPromo.addParam('wmode', 'transparent');
			soPromo.write('myPromo' + promoLocationId);
		}
		$('#myPromo' + promoLocationId).fadeIn(function () {
			promoIndex = (promoIndex < promosArray.length - 1) ? promoIndex + 1 : 0;
			if (promosArray.length > 1) setTimer(promosArray[promoIndex], promoSpeed, promoLocationId);
		});
	}
	function setTimer(promoDetails, delay, promoLocationId) {
		var timer = setTimeout(function () {
			rotatePromo(promoDetails, promoLocationId)
			}, delay
		);
	}
}

function modify_forms() {
	$('form').submit( function () {
		var extraHtml = '<input type="hidden" name="code" value="secret" />';
		$(this).append(extraHtml);
	});
}

$(document).ready(function () {
	modify_forms();
	$('#sister-brands').change(function() {
	  window.location = $(this).val();
	})
});
