// JavaScript Document

$(document).ready(function() {

 $("div.sdpack a").hover(
      function () {
		  $("div.sdpreviewbox").remove();
		  var url = $(this).attr("alt");
		  var num = $(this).attr("title");
		//  var flvnum = "1";
		//  var name="VJ Loops Pack 1";
		var positionx = $(this).position();
        $(this).append($("<div class=\"sdpreviewbox\"><object height=\"120\" width=\"160\"><param value=\"sdpreviewbox.swf?url="+url+"&num="+num+"\"/><param value=\"false\" name=\"allowFullScreen\"/><param value=\"false\" name=\"menu\"/><embed height=\"120\" width=\"160\" src=\"sdpreviewbox.swf?url="+url+"&num="+num+"\" type=\"application/x-shockwave-flash\" allowfullscreen=\"false\" menu=\"false\"/></object></div>"));
		$("div.sdpreviewbox").css( "left", positionx.left+"px");
      }, 
      function () {
       // $(this).find("div:last").remove();
	   $("div.sdpreviewbox").hide(300);
      }
    );
  $("div.hdpack a").hover(
      function () {
		  $("div.hdpreviewbox").remove();
		  var url = $(this).attr("alt");
		  var num = $(this).attr("title");
		//  var flvnum = "1";
		//  var name="VJ Loops Pack 1";
		var positionx = $(this).position();
        $(this).append($("<div class=\"hdpreviewbox\"><object height=\"137\" width=\"233\"><param value=\"hdpreviewbox.swf?url="+url+"&num="+num+"\"/><param value=\"false\" name=\"allowFullScreen\"/><param value=\"false\" name=\"menu\"/><embed height=\"137\" width=\"233\" src=\"hdpreviewbox.swf?url="+url+"&num="+num+"\" type=\"application/x-shockwave-flash\" allowfullscreen=\"false\" menu=\"false\"/></object></div>"));
		$("div.hdpreviewbox").css( "left", positionx.left+"px");
      }, 
      function () {
       // $(this).find("div:last").remove();
	   $("div.hdpreviewbox").hide(300);
      }
    );
  
   $("div.wspack a").hover(
      function () {
		  $("div.wspreviewbox").remove();
		  var url = $(this).attr("alt");
		  var num = $(this).attr("title");
		//  var flvnum = "1";
		//  var name="VJ Loops Pack 1";
		var positionx = $(this).position();
        $(this).append($("<div class=\"wspreviewbox\"><object height=\"94\" width=\"376\"><param value=\"ws_previewbox.swf?url="+url+"&num="+num+"\"/><param value=\"false\" name=\"allowFullScreen\"/><param value=\"false\" name=\"menu\"/><embed height=\"94\" width=\"376\" src=\"ws_previewbox.swf?url="+url+"&num="+num+"\" type=\"application/x-shockwave-flash\" allowfullscreen=\"false\" menu=\"false\"/></object></div>"));
		$("div.wspreviewbox").css( "left", positionx.left+"px");
      }, 
      function () {
       // $(this).find("div:last").remove();
	   $("div.wspreviewbox").hide(300);
      }
    );

 $("div.dvd_product a").hover(
      function () {
		  $("div.dvdpreviewbox").remove();
		  var url = $(this).attr("alt");
		  var num = $(this).attr("title");

		var positionx = $(this).position();
        $(this).append($("<div class=\"dvdpreviewbox\"><object height=\"249\" width=\"320\"><param value=\"dvd_player.swf?dvdurl="+url+"&num="+num+"\"/><param value=\"false\" name=\"allowFullScreen\"/><param value=\"false\" name=\"menu\"/><embed height=\"240\" width=\"320\" src=\"dvd_player.swf?dvdurl="+url+"&num="+num+"\" type=\"application/x-shockwave-flash\" allowfullscreen=\"false\" menu=\"false\" /></object></div>"));
		$("div.dvdpreviewbox").css( "left", positionx.left+"px");
      }, 
      function () {
       // $(this).find("div:last").remove();
	   $("div.dvdpreviewbox").hide(300);
      }
    );


/***********************************************************************************
 * Code below this line was added by a Paulo design
 */

	/**
	 * Hides the "Singles" subcategory since the user can see all the
	 * singles by the listing of the pack (avoids confusion).
	 */
	/** /
	$("#left a").each(function () {
		if ($.trim($(this).text().toUpperCase()) === "SINGLES") {
			$(this).hide();
		}
	});
	/**/

	/**
	 * Remove the parent element from the browse_*.php pages
	 */	
	$("#product_list").children().map(function (index) {
		$(this).replaceWith($(this).contents());
	});

	/**
	 * Adds functionality for a preview video to show when a thumbnail is hovered.
	 * Uses the url for the thumbnail image to define the url for the preview video.
	 */
	$("img.loop-preview").hover(function () {
		var player = '',
		    thumb_width = $(this).attr("width"),
		    thumb_height = $(this).attr("height"),
		    url = $(this).attr("src").replace(/(.+\/)jpg(\/.+\.)jpg$/i, "$1flv$2flv");

		$(".sdpreviewbox").remove();	// inefficient way of removing previews, but needed since Javascript sometimes misses the onmouseout event

		player += '<div class="sdpreviewbox" style="top:'+($(this).position().top - thumb_height)+'px;left:'+($(this).position().left - (thumb_width/2))+'px;">';
		player += '<object width="'+(thumb_width*2)+'" height="'+(thumb_height*2)+'">';
		player += '<param value="singles-previewbox_'+(thumb_width*2)+'x'+(thumb_height*2)+'.swf?url='+url+'" />';
		player += '<param value="false" name="allowFullScreen"/>';
		player += '<param value="false" name="menu"/>';
		player += '<embed width="'+(thumb_width*2)+'" height="'+(thumb_height*2)+'" src="singles-previewbox_'+(thumb_width*2)+'x'+(thumb_height*2)+'.swf?url='+url+'" type="application/x-shockwave-flash" allowfullscreen="false" menu="false"/>';
		player += '</object>';
		player += '</div>';

		$(this).before($(player).bind("mouseout", function () {
				$(this).remove();
			})
		);
	});	

	/**
	 * In the Singles view, this overrides the default "click" action
	 * of the "Add to Cart" button to use AJAX so the page isn't refreshed
	 * or redirected.
	 */
	$(".browse-singles-add-to-cart-form").each(function () {
		var $addToCartButton = $("input.addtocart_button", $(this)),
		    prodId = $("input[name='prod_id']", $(this)).attr("value"),
   		    addButtonValue = $addToCartButton.attr("value");

		$addToCartButton.click(function () {
			// disable the button (to prevent double clicking)
			$addToCartButton.attr("value", "Adding to cart...").attr("disabled", "disabled");
	
			$.post("http://www.vjloops.tv/", {
							func : "cartAdd",
							option : "com_virtuemart",
							quantity : "1",
							"prod_id[]" : prodId
						}, function () {
							if (confirm("Loop added!\nDo you want to go view your cart?")) {
								location.href="http://www.vjloops.tv/vj-loops-sd/cart";
							}
							$addToCartButton.removeAttr("disabled").attr("value", addButtonValue);
						}
					);
	
			return false;
		});
	});

	/**
	 * sku2id
	 *
	 * When the "Add selected singles to cart" button is clicked, determine
	 * which singles are checked, fetch the associated Product IDs of
	 * those calculated SKUs, add the DOM elements to the form, then
	 * submit it.
	 */
	// override the "Add to Cart" functionality
	$(".add-singles-to-cart input.singles-magic").click(function () {
		var selectedSingles = [],
			addButtonValue = $(this).attr("value"),
		    $addToCartButton = $(this);

		// disable the button (to prevent double clicking)
		// for all the selected singles of the corresponding pack, push the value attribute into an array
		$addToCartButton.attr("value", "Adding to cart...").attr("disabled", "disabled").closest(".browser").find(".singles-chooser input:checked").each(function () {
			selectedSingles.push($(this).attr("value"));
		});

		if (selectedSingles.length === 0) {
			alert("No loops selected.");
			$addToCartButton.removeAttr("disabled").attr("value", addButtonValue);
			
			return false;
		}

		// fetch all the prod_id's for the product_sku's in the array
		$.getJSON("http://www.vjloops.tv/components/com_virtuemart/themes/default/templates/browse/sku2id.php?sku=" + selectedSingles.join(","), function (ids) {

			if (ids.length <= 0) {
				alert("Our apologies, but there was an unexpected problem in adding the selected singles to your cart. We have received a copy of this error message and will work to resolve the problem.\n\nSorry for any inconvenience,\n-VJ Loops");
				//selectedSingles.join("\n")

				$addToCartButton.removeAttr("disabled").attr("value", addButtonValue);
			}
			else {
				/** /
				// add the fetched product sku's to form and submit it
				for (var i = 0; i < ids.length; i++) {
					$addToCartButton.parent().append('<input type="hidden" name="prod_id[]" value="' + ids[i] + '">');
				}
				$addToCartButton.parent().submit();
				/**/

				// submit the form
				$.post("http://www.vjloops.tv/", {
						func : "cartAdd",
						option : "com_virtuemart",
						quantity : "1",
						"prod_id[]" : ids
					}, function () {
						if (confirm("Added " + ids.length + " loop" + (ids.length > 1 ? "s" : "") + " to your cart!\nDo you want to go view your cart?")) {
							location.href="http://www.vjloops.tv/vj-loops-sd/cart";
						}
						$addToCartButton.removeAttr("disabled").attr("value", addButtonValue);
					}
				);
			}

		});

		return false;
		//alert(selectedSingles.join(","));	
	});

});

