function writeFlash(url,w,h,id,bg,trans) {
var html = "";
html += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" width="' + w + '" height="' + h + '" id="'+ id + '">\n';
html += '<param name="movie" value="' + url + '" />\n';
html += '<param name="quality" value="high" />\n';
html += '<param name="bgcolor" value="' + bg + '" />\n';
html += '<param name="scale" value="noscale" />\n';
if (trans == 1) {html += '<param name="wmode" value="transparent" />\n';}
html += '<!--[if !IE]> <-->\n';
html += '<object data="' + url + '" width="' + w + '" height="' + h + '" type="application/x-shockwave-flash" name="'+ id + '">\n';
html += '<param name="quality" value="high" />\n';
html += '<param name="scale" value="noscale" />\n';
html += '<param name="bgcolor" value="' + bg + '" />\n';
html += '<param name="pluginurl" value="http://www.macromedia.com/go/getflashplayer" />\n';
if (trans == 1) {html += '<param name="wmode" value="transparent" />\n';}
html += ' FAIL (the browser should render some flash content, not this).\n';
html += ' </object>\n';
html += '<!--> <![endif]-->\n';
html += '</object>\n';
document.write(html);
}
//function visit(newURL) {
//	var site = window.open(newURL, 'website','scrollbars=1, resizable=1, location=1, menubar=1, status=1, toolbar=1, height=768, width=1024');
//	site.window.focus();
//}
function popup(newURL) {
	var p = window.open(newURL, 'popup','scrollbars=0, resizeable=0, width=425, height=550');
	p.window.focus();
}
function popupPic(sPicURL) {
	var pp = window.open( "popup.html?"+sPicURL, "popupPic", "scrollbars=0, resizeable=1, width=425, height=550");
	pp.window.focus();
}
function rand(min,max){
	var $min,$max,random_num;
	$min = min;
	$max = max + 1;
	random_num = Math.floor($min + ($max-$min)*Math.random());
	return random_num;
}
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
};
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
};
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
};
Array.prototype.indexOf = function( v, b, s ) {
 for( var i = +b || 0, l = this.length; i < l; i++ ) {
  if( this[i]===v || s && this[i]==v ) { return i; }
 }
 return -1;
};

function verticalCenter(target){
	var wH = $(window).height();
	var oH = $(target).height();
	if ( wH <= oH ){
		$(target).css({
			top: "10px",
			marginTop: "0px"
		});
	}
	else{
		$(target).css({
			top: "50%",
			marginTop: "-350px"
		});
	}
}

function URLEncode (clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
    	output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}

$(document).ready(function(){
	$("#content").pngFix();
	
	//external link setup
//	$("a[href^='http://']").not(".cart").each(function(){
//		$(this).attr({
//			href: "javascript:visit('" + $(this).attr('href') + "')"
//		});
//	});
	//background rollovers setup
	$(".rollover").hover(
		function () {
			var h = "0px -" + $(this).css("height");
			$(this).css("background-position",h);
		},
		function () {
			$(this).css("background-position","0px 0px");
		}
	);
	//mailto setup
	$(".mailto").each(function(){
		var name = $(this).attr("title");
		var html = "<a href='mailto:" + name + "@paw-purrbling.com'>" + name + "@paw-purrbling.com</a>";
		$(this).html(html);
	});
	//vertical center site
	verticalCenter("#outerWrapper");
	//cart
	$("a.cart").click(function() {
		//$(this).removeAttr("href");

		// td
		var crystal = $(this).parent().parent().parent();
		var error = "";

		// 1
		var product_option = $(crystal).children("p.description").find("select[name='ProductOption']").val();
		var product_option_text = $(crystal).children("p.description").find("select[name='ProductOption'] option:selected").text();
		var product_option_sel = $(crystal).children("p.description").find("select[name='ProductOption']");

		// 2
		var product_add_option = $(crystal).children("p.description").find("select[name='ProductAddOption']").val();
		var product_add_option_text = $(crystal).children("p.description").find("select[name='ProductAddOption'] option:selected").text();
		var product_add_option_sel = $(crystal).children("p.description").find("select[name='ProductAddOption']");

		// 3
		var product_style = $(crystal).children("p.description").find("select[name='ProductStyle']").val();
		var product_style_text = $(crystal).children("p.description").find("select[name='ProductStyle'] option:selected").text();
		var product_style_sel = $(crystal).children("p.description").find("select[name='ProductStyle']");

		// 4
		var product_style2 = $(crystal).children("p.description").find("select[name='ProductStyle2']").val();
		var product_style2_text = $(crystal).children("p.description").find("select[name='ProductStyle2'] option:selected").text();
		var product_style2_sel = $(crystal).children("p.description").find("select[name='ProductStyle2']");

		// 5
		var product_color = $(crystal).children("p.description").find("select[name='ProductColor']").val();
		var product_color_text = $(crystal).children("p.description").find("select[name='ProductColor'] option:selected").text();
		var product_color_sel = $(crystal).children("p.description").find("select[name='ProductColor']");

		if (product_option == -1)
		{
			$(product_option_sel).css("background-color", "#fcf");
			error += "- You must select Product Options!\n";
		}
		else
		{
			$(product_option_sel).css("background-color", "transparent");
		}

		if (product_style == -1)
		{
			$(product_style_sel).css("background-color", "#fcf");
			error += "- You must select Crystal Style!\n";
		}
		else
		{
			$(product_style_sel).css("background-color", "transparent");
		}

		if (product_style2 == -1)
		{
			$(product_style2_sel).css("background-color", "#fcf");
			error += "- You must select Crystal Style!\n";
		}
		else
		{
			$(product_style2_sel).css("background-color", "transparent");
		}

		if (product_add_option == -1)
		{
			$(product_add_option_sel).css("background-color", "#fcf");
			error += "- You must select Additional options!\n";
		}
		else
		{
			$(product_add_option_sel).css("background-color", "transparent");
		}

		if (product_color == -1)
		{
			$(product_color_sel).css("background-color", "#fcf");
			error += "- You must select Color Options!\n";
		}
		else
		{
			$(product_color_sel).css("background-color", "transparent");
		}

		if (error.length > 0)
		{
			alert(error);
		}
		else
		{
			var regexp = /^\$(.+?)\s/i;
			var price = regexp.exec($(this).parent().text());
			var product_name = $(this).attr("title");
			var product = "";

			if (product_option != null) product += ", " + product_option_text;
			if (product_style != null) product += ", " + product_style_text;
			if (product_style2 != null) product += ", " + product_style2_text;
			if (product_add_option != null) product += ", " + product_add_option_text;
			if (product_color != null) product += ", " + product_color_text;

			product_name += product;

			top.location = "http://ww9.aitsafe.com/cf/add.cfm?userid=B2163173&product=" + URLEncode(product_name) + "&price=" + price[1].toString() + "&return=http://www.paw-purrbling.com/";
		}

		return false;
	});
	//contact
	$("#contact").submit(function()
	{
		$("#contact > p").attr("class", "info");
		$("#contact > p").text("Sending...");
		$("#contact > p").fadeIn("normal");

		$.post(
			"_scripts/ajax.contact.php", {
				sended: "1",
				name: $("input#name").val(),
				city: $("input#city").val(),
				email: $("input#email").val(),
				phone: $("input#phone").val(),
				hear: $("textarea#hear").val()
			}, function(data) {
				var answer = data.toString().split("  ");

				$("#contact > p").attr("class", answer[0]);
				$("#contact > p").text(answer[1]);

				setTimeout(function() {
					$("#contact > p").fadeOut("normal");
				}, 3500);
			}
		);

		return false;
	});
});