var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false; var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;

function init_page(){
	var $n = jQuery("a.pr-write-review-link");
	if (!$n.length) $n = jQuery("a.pr-snippet-link");
	if ($n.length) $n.click(on_click_write_review);
}

function on_click_write_review(e){
	var sku = jQuery("input[name='parent_sku']").attr("value");
	if (typeof(sku)!=="string") sku = jQuery("input[name='sku']").attr("value");
	return _gaq.push(['_trackEvent', 'Products', 'Write a Review', sku]);
}

function AddToCart(f, iStock){
	//if (iStock != "NO"){
		return true;
	//}else{
	//	alert("This product is currently out of stock. Please try back later!");
	//	return false;
	//}
}

var prod_wndw;
function ProductPopup(url){
	prod_wndw = util.PopupWndw(url, "prod_wndw", prod_wndw, 630, 650, 1, 1, 1, 0, 0, 0);
}

getViewportWidth = function() {
  var width = 0;
  if( document.documentElement && document.documentElement.clientWidth ) {
	width = document.documentElement.clientWidth;
  }
  else if( document.body && document.body.clientWidth ) {
	width = document.body.clientWidth;
  }
  else if( window.innerWidth ) {
	width = window.innerWidth - 18;
  }
  return width;
}

getViewportHeight = function() {
  var height = 0;
  if( document.documentElement && document.documentElement.clientHeight ) {
	height = document.documentElement.clientHeight;
  }
  else if( document.body && document.body.clientHeight ) {
	height = document.body.clientHeight;
  }
  else if( window.innerHeight ) {
	height = window.innerHeight - 18;
  }
  return height;
}

getViewportScrollY = function() {
  var scrollY = 0;
  if( document.documentElement && document.documentElement.scrollTop ) {
	scrollY = document.documentElement.scrollTop;
  }
  else if( document.body && document.body.scrollTop ) {
	scrollY = document.body.scrollTop;
  }
  else if( window.pageYOffset ) {
	scrollY = window.pageYOffset;
  }
  else if( window.scrollY ) {
	scrollY = window.scrollY;
  }
  return scrollY;
}

function show_video_modal(){
	var vid_url = "http://www.youtube.com/v/"+ youtube_vid_id;
	osx_dialog.show("Video Clip", "<p align=\"center\">\
	<!--HTML 5 no autoplay \
		<iframe title=\"YouTube video player\" class=\"youtube-player\" type=\"text/html\" \
			src=\""+ vid_url +"\" \
			width=\"560\" height=\"420\" frameborder=\"0\" ></iframe>\
	-->\n\
	<object width=\"560\" height=\"420\">\
		<param name=\"movie\" value=\""+ vid_url +"?fs=1&amp;hl=en_US&amp;autoplay=1\"></param>\
		<param name=\"allowFullScreen\" value=\"true\"></param>\
		<param name=\"allowscriptaccess\" value=\"always\"></param>\
		<embed src=\""+ vid_url +"?fs=1&amp;hl=en_US&amp;autoplay=1\" \
			type=\"application/x-shockwave-flash\" \
			allowscriptaccess=\"always\" \
			allowfullscreen=\"true\" \
			width=\"560\" height=\"420\">\
		</embed>\
	</object>\
	</p>");
}

function reShowVeil() {
	if(document.getElementById('veil').style.display == 'block') loadVeil();
}

function hideVeil() {
	var veil = document.getElementById('veil');
	if(veil.style.display == 'block') veil.style.display = 'none';
	var a;
	if(!document.getElementById('dvLargeImage')) {
		a = document.createElement('DIV');
		a.id = 'dvLargeImage';
		document.body.appendChild(a);
	}
	a = document.getElementById('dvLargeImage');
	a.style.display = 'none';
}
function loadVeil() {
	var veil = document.getElementById('veil');
	veil.style.height = (document.body.offsetHeight > document.body.scrollHeight ? document.body.offsetHeight : document.body.scrollHeight)+'px';
	veil.style.width = 	(isIE ? document.body.clientWidth : document.width)+'px';
	veil.style.filter = 'alpha(opacity=50)';
	veil.style.top = 0;
	veil.style.left = 0;
	veil.style.display = 'block';
}
function viewWindow(itemedp,itemdesc,ntype) {
  if (typeof(youtube_vid_id)=="string" && ntype == '2'){
		show_video_modal();
  } else {
	var a, imgHTML, divWidth, divHeight;
	if (ntype == '1'){
		divWidth = 520;
		divHeight = 580;
		imgHTML = ""
		imgHTML = imgHTML + "<div id='headerV'><span class='titleV'>Product View</span> <span class='closeV'>Close <a href='javascript:hideVeil()'><img src='http://cdn.taylorgifts.com/images/tg/btn_close.jpg'></a></span></div>";
		imgHTML = imgHTML + "<div><img src='http://cdn.taylorgifts.com/images/products/p" + itemedp + "_500.jpg' width=500 height=500 class='imgV'></div>";
		imgHTML = imgHTML + "<div class='titleV'>" + itemdesc + "</div>";
	}
	if (ntype == '2'){
		imgHTML = ""
		imgHTML = imgHTML + "<div id='headerV'><span class='titleV'>Watch Video</span> <span class='closeV'>Close <a href='javascript:hideVeil()'><img src='http://cdn.taylorgifts.com/images/tg/btn_close.jpg'></a></span></div>";
		if (PgConf.site=="TG"){
			divWidth = 320;
			divHeight = 340;
			imgHTML = imgHTML + "<div id=\"flashbox\" style=\"width:320px; height:265px;\"></div>";
		}else{
			divWidth = 520;
			divHeight = 520;
			imgHTML = imgHTML + "<div id=\"flashbox\" style=\"margin-left:5px; width:506px; height:420px;\"></div>";
		}
	}
	var setX = ( getViewportWidth() - divWidth ) / 2;
	var setY = ( getViewportHeight() - divHeight ) / 2;
	if( setX < 0 ) setX = 0;
	if( setY < 0 ) setY = 0;
	loadVeil();
	if(!document.getElementById('dvLargeImage')) {
		a = document.createElement('DIV');
		a.id = 'dvLargeImage';
		document.body.appendChild(a);
	}
	a = document.getElementById('dvLargeImage');
	a.innerHTML = imgHTML
	a.style.left = setX + "px";
	a.style.top = getViewportScrollY() + setY + "px";
	a.style.display = 'block';
	if (ntype == '2'){
		util.add_flash_video("flashbox", "/flash/" + itemedp + ".flv", true);
	}
  }
}
function switchtoVideo(division) {
  if (typeof(youtube_vid_id)=="string"){
		show_video_modal();
  } else {
	document.getElementById("defaultimg").innerHTML = "<a href=" + "javascript:switchtoImage('" + division + "')" + " title=" + "View Image" + ">View Image</a>";
	document.getElementById("defaultimg").style.background = 'transparent url("../../images/' + division + '/bg_prodtab.gif") no-repeat scroll -1px top';
	document.getElementById("video").innerHTML = 'Watch Video';
	document.getElementById("video").style.background = 'transparent url("../../images/' + division + '/bg_prodtab-active.gif") no-repeat scroll left top';
	document.getElementById("showcase_view_01").style.display = 'none';
	util.show_flash_video("flashbox");	//use this to avoid firefox/flash glitches
	//document.getElementById("flashbox").style.display = 'block';
  }
}
function switchtoImage(division) {
	document.getElementById("defaultimg").innerHTML = 'View Image';
	document.getElementById("defaultimg").style.background = 'transparent url("../../images/' + division + '/bg_prodtab-active.gif") no-repeat scroll left top';
	document.getElementById("video").innerHTML = "<a href=" + "javascript:switchtoVideo('" + division + "')" + " title=" + "Watch Video" + ">Watch Video</a>";
	document.getElementById("video").style.background = 'transparent url("../../images/' + division + '/bg_prodtab.gif") no-repeat scroll -1px top';
	document.getElementById("showcase_view_01").style.display = 'block';
	util.hide_flash_video("flashbox");	//use this to avoid firefox/flash glitches
	//document.getElementById("flashbox").style.display = 'none';
}
function watchVideo(itemedp)
{
	loadVeil();
}
function showAvail(num) {
	for (i=0; i<=20; i++)
	{
		if (document.getElementById('checkavail_' + i)) {
			document.getElementById('checkavail_' + i).style.visibility = 'hidden';
		}
	}
	if (document.getElementById('checkavail_' + num))
	{
		document.getElementById('checkavail_' + num).style.visibility = "visible";
	}
}
function closeAvail(num) {
	if (document.getElementById('checkavail_' + num))
	{
		document.getElementById('checkavail_' + num).style.visibility = "hidden";
	}
	if (document.getElementById('availemail_' + num))
	{
		document.getElementById('availemail_' + num).style.display = 'block';
	}
	if (document.getElementById('availimage_' + num))
	{
		document.getElementById('availimage_' + num).style.display = 'block';
	}
	document.getElementById('notifymessage_' + num).innerHTML = 'Enter your email address below to be notified when this item becomes available';
}
function showTellFriend() {
	if (document.getElementById('tellfriend'))
	{
		document.getElementById('tellfriend').style.visibility = "visible";
	}
}
function closeTellFriend() {
	if (document.getElementById('tellfriend'))
	{
		document.getElementById('tellfriend').style.visibility = "hidden";
	}
	if (document.getElementById("tellfriendmessage"))
	{
		document.getElementById("tellfriendmessage").style.display = 'none';
	}
	if (document.getElementById("tellemail"))
	{
		document.getElementById("tellemail").style.display = 'block';
	}
	if (document.getElementById("tellimage"))
	{
		document.getElementById("tellimage").style.display = 'block';
	}
}
function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
	    {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	    }
		catch (e)
	    {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	    }
	}
	return xmlHttp;
}
function ajxFrmProdNotify(num)
{
	var sku = alltrim(document.getElementById("sku").value);
	var email = alltrim(document.getElementById('availemail_' + num).value);
	var url = "/ajaxed/product-emailnotify.asp?sku="+sku+"&email="+email;
	xmlhttp = new GetXmlHttpObject();
	xmlhttp.open('GET',url,true);
	xmlhttp.onreadystatechange=function() {
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			document.getElementById('notifymessage_' + num).innerHTML = 'Thanks! You will be emailed when this product is available.';
			if (document.getElementById('availemail_' + num))
			{
				document.getElementById('availemail_' + num).style.display = 'none';
				document.getElementById('availemail_' + num).value = 'Email Address';
			}
			if (document.getElementById('availimage_' + num))
			{
				document.getElementById('availimage_' + num).style.display = 'none';
			}
		}
	}
	xmlhttp.send(null);
}
function ajxFrmProdTellFriend()
{
	var email = alltrim(document.getElementById("tellemail").value);
	var url = "/ajaxed/product-emailfriend.asp?email="+email;
	xmlhttp = new GetXmlHttpObject();
	xmlhttp.open('GET',url,true);
	xmlhttp.onreadystatechange=function() {
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			document.getElementById('tellfriendmessage').innerHTML = 'Thanks! You friend will be emailed about this great product shortly.';
			if (document.getElementById("tellfriendmessage"))
			{
				document.getElementById("tellfriendmessage").style.display = 'block';
			}
			if (document.getElementById("tellemail"))
			{
				document.getElementById("tellemail").style.display = 'none';
				document.getElementById("tellemail").value = 'Email Address';
			}
			if (document.getElementById("tellimage"))
			{
				document.getElementById("tellimage").style.display = 'none';
			}
		}
	}
	xmlhttp.send(null);
}
function addwishlist(num)
{
	var frm = document.getElementById('frmProduct_' + num)
	if (frm)
	{
	}else{
		frm = document.getElementById('frmProduct');
	}
	var fvalid = "Y"
	if ((frm.quantity.value == "") && (fvalid == "Y"))
	{
		alert("Please enter a Quantity");
		frm.quantity.value = "";
		frm.quantity.focus();
		fvalid = "N"
	}
	if ((!IsNumeric(frm.quantity.value)) && (fvalid == "Y"))
	{
		alert("Please enter a Quantity");
		frm.quantity.value = "";
		frm.quantity.focus();
		fvalid = "N"
	}
	if (fvalid == "Y")
	{
		cururl = '/addwishlist.asp?sku=' + frm.sku.value + '&qty=' + frm.quantity.value; 
		window.location.assign( cururl ); 
	}
}


var osx_dialog = {
	container: null,
	show: function (title, message) {
		$("#osx-modal-title").html(title);
		$("#osx-modal-data").html(message);
		$("#osx-modal-content").modal({
			overlayId: "osx-overlay",
			containerId: "osx-container",
			closeHTML: null,
			minHeight: 480,
			opacity: 40, 
			position: ["0",],
			overlayClose: false,
			onOpen: osx_dialog.open,
			onClose: osx_dialog.close
		});
	},
	open: function (d) {
		var self = this;
		self.container = d.container[0];
		d.overlay.fadeIn(200, function () {
			$("#osx-modal-content", self.container).show();
			var title = $("#osx-modal-title", self.container);
			title.show();
			d.container.slideDown(300, function () {
				setTimeout(function () {
					var h = $("#osx-modal-data", self.container).height()
						+ title.height()
						+ 20; // padding
					d.container.animate(
						{height: h}, 
						200,
						function () {
							$("div.close", self.container).show();
							$("#osx-modal-data", self.container).show();
						}
					);
				}, 300);
			});
		})
	},
	close: function (d) {
		var self = this; // this = SimpleModal object
		d.container.animate(
			{top:"-" + (d.container.height() + 20)},
			400,
			function () {
				self.close(); // or $.modal.close();
			}
		);
	}
};

