// hover class
(function($) {
    $.fn.hoverClass = function(c) {
        return this.each(function() {
            $(this).hover(
                function() { $(this).addClass(c); },
                function() { $(this).removeClass(c); }
            );
        });
    };
})(jQuery);

var openwin = function(url) {
	var popupwin = window.open(url,'bbs_popup','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=450,height=280,screenX=150,screenY=150,top=150,left=150');
}

var xe = function(qs) {
	var currency_win = window.open ('', 'CurrencyWindow', 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,height=170,width=600');
	currency_win.focus();
	currency_win.location.href = 'http://www.xe.com/ecc/input.cgi?Template=sw&'+ qs;
}

var update = function() {
	$("#btn-update input").click();
}

$(function() {
	var $last = $("#breadcrumbs li:last"),
		width = $last.addClass("active").width() + 1,
		breadcrumbHTML = $.trim($last.text());
	
	$last.width(width + 5).html('<span class="text"">'+ breadcrumbHTML +'</span><span class="cap"></span>');
	
	$("#sidebar li.navLevel1:last").addClass("last");
	
	$("#images img").hoverClass("over");
	
	$(":password, :text").addClass("text")
	.bind("focus", function() {
		$(this).addClass("focus");
	}).bind("blur", function() {
		$(this).removeClass("focus");
	});
	
	$("a[href][rel='external']").attr("target","_blank");
	
	$(".actions input[type='image']").click(function() {
		$this = $(this);
		$select = $this.parent().prev().find("select");
		
		if($select.val() == "0") {
			alert("Please make sure you have made a selection from one of the options!");
			return false;
		}
	});
	
	if($(".status").length > 0) {
		$(".status").each(function() {
			var $this = $(this);
			var status = "prod"+ $this.val().split(" ").join("").toLowerCase();
			$this.siblings(".image").append('<div class="'+ status +' banner" />');
		});
	}
	
	if($("#status").length > 0) {			
		var status = "prod"+ $("#status").val().split(" ").join("").toLowerCase();
		$("#image").append('<div class="'+ status +' banner" />');
	}
	
	$("#images img").click(function() {
		var $this = $(this);
		var $img = $("#image img");
		var src = $this.attr("src");
		var osrc = $img.attr("src");
		
		$this.attr("src", osrc);
		$img.attr("src", src);

	});
	
	// setup events for hiding spans/text in block labels
    $("label.block *").bind("click focus", function(e) {
        var $label = $(e.target).parent();

        if ($label.find("input").val() == "") {
            $label.find("span").addClass("hide");
            $label.find("input").get(0).focus();
        } else if ($label.find("input").val() != "") {
            $label.find("span").addClass("hide");
            $label.find("input").get(0).focus();
        } else {
            $label.find("span").removeClass("hide");
        }
    });
    $("label.block input").bind("blur", function(e) {
        var $label = $(e.target).parent();
        var $this = $(this);

        if ($this.val() == "") {
            $label.find("span").removeClass("hide");
        } else {
            $label.find("span").addClass("hide");
        }
    });
	
	$("#convert a").click(function(e) {
		e.preventDefault();
		var val = $("#to :selected").val();
		var price = $("#special").text() != "" ? $("#special span").text().substr(1) : $("#regular").text().substr(8);
		if(val != "NONE") {
			var url = "http://www.google.com/finance/converter?a="+ price +"&from=AUD&to="+ val;
		} else {
			var url = "http://www.google.com/finance/converter?a="+ price +"&from=AUD&to=USD";
		}
		var currency = window.open(url, "currency", "status=yes,resizable=yes,width=290,height=185");
	});
	
	$("#to").bind("change", function() {
		var val = $("#to :selected").val();
		if(val != "NONE") {
			var price = $("#special").text() != "" ? $("#special span").text().substr(1) : $("#regular").text().substr(8);
			var url = "http://www.google.com/finance/converter?a="+ price +"&from=AUD&to="+ val;
			var currency = window.open(url, "currency", "status=yes,resizable=yes,width=290,height=185");
		}
	});
	
	if($("input[value=paypal]").length > 0) {
		$("input[value=paypal]").click(function() {
			if($("#paypal-message").length == 0) {
				$("tr.moduleRow:eq(0)").after('<tr id="paypal-message"><td width="10"/><td class="main stockWarning" colspan="4"><p>If you continue to pay with PayPal, please be sure to return to our site by clicking the &ldquo;Return to Burleighbodyboardshop.com.au&rdquo; button on the PayPal site once your payment goes through! Thanks!</p></td><td width="10"/></tr>');
			} else {
				$("#paypal-message").show();
			}
		})
		$("input[value!=paypal]").click(function() {
			$("#paypal-message").hide();
		});
	}
		   
	if($("#google_map").length > 0 && GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("google_map"));
		var BBS = new GIcon();

		var BBSOpts = {
			image: "/catalog/common/images/bbs-icon.png",
			shadow: "/catalog/common/images/bbs-icon-shadow.png",
			iconSize: new GSize(150.0, 44.0),
			shadowSize: new GSize(173.0, 44.0),
			iconAnchor: new GPoint(75.0, 22.0),
			infoWindowAnchor: new GPoint(75.0, 22.0)
		}

		jQuery.extend(BBS, BBSOpts);
		var SHOP = new GMarker(new GLatLng(-28.088583548303916, 153.45396280288696), BBS);
		
		map.setCenter(new GLatLng(-28.088583548303916, 153.45396280288696), 15); 
		map.addOverlay(SHOP);
	}
	
	$("#plug-launch").colorbox({ width: "420px", inline: true, href: "#disclaimer" }); 
	$("#plug-continue").colorbox({ innerWidth: "420px", innerHeight: "525px", inline: true, href: "#board" });
	$("#plug-confirmed").colorbox({ innerWidth: "420px", innerHeight: "120px", inline: true, href: "#confirmed", onLoad: function() {
		$.ajax({
			url: "sendmail.php",
			data: { top: top, left: left, order_id: $("#order_id_plug").val() },
			dataType: "json",
			success: function(data) {
				if(data.sent == "true") {
					console.log("YUP!");
					$("#confirmed p").hide().html("Your plug position has been sent, if there are any issues we&rsquo;ll be in touch!").fadeIn(500);
				} else {
					$("#confirmed p").hide().html("Sorry, but there has been an error confirming your plug position.<br />Please give us a call on (07) 5520 3332.<br />Thanks!").fadeIn(500);
				}
			}
		});
	}});
	$("#plug-reposition").colorbox({ innerWidth: "420px", inline: true, href: "#board" });
	
	var int, top, left;

	$("#plug").bind('drag',	function( event ){
		clearTimeout(int);
		if(event.offsetY) {
			var offset = $("#board").offset();
			$(this).css({ 
				top: event.offsetY - offset.top,
				left: event.offsetX - offset.left
			});
		}
	}).bind('dragend', function() {
		//console.log("top: "+ $(this).css("top") +" / left: "+ $(this).css("left"));
		top = $(this).css("top");
		left = $(this).css("left");
		int = setTimeout(function() { $.fn.colorbox({ innerWidth: "420px", inline: true, href: "#confirm" }) }, 1000);
	});
	
});
