﻿/// <reference path="jquery-vsdoc.js" />

//===============================================================================
// For having an appPath in JavaScript
//===============================================================================
var appPath = "/";
function SetAppPath(path)
{
  appPath = path;
}

var MERCHANT_URL = "http://melbournepar108.corecommerce.com/";

//===============================================================================
// For automatic replacement of hoverable (and clickable) images
// Usage:
//   Images must be named xxx.ext, xxx-hover.ext, xxx-down.ext (optional)
//   and must be in the same directory
//   eg. submit.png, submit-hover.png, submit-down.png (optional)
// On the .html or .aspx:
//   <img class="imgHoverable" src="<%=AppPath%>images/submit.png"/>
// or
//   <img class="imgHoverable imgPushable" src="<%=AppPath%>images/submit.png"/>
//
// These functions below preloads the mouseover and mousedown images,
// and hook the events to replace images on mouseover/mousedown automatically
//===============================================================================
$(document).ready(function() {
  // Change the image of hoverable images
	$(".imgHoverable").hover( function() {
			var hoverImg = HoverImgOf($(this).attr("src"));
			$(this).attr("src", hoverImg);
		}, function() {
			var normalImg = NormalImgOf($(this).attr("src"));
			$(this).attr("src", normalImg);
		}
	);

		// Change the background image of hoverable buttons
		$(".btnHoverable").hover(function() {
		  var hoverImg = HoverImgOf($(this).css("background-image"));
		  $(this).css("background-image", hoverImg);
		}, function() {
		  var normalImg = NormalImgOf($(this).css("background-image"));
		  $(this).css("background-image", normalImg);
		}
	);

		// Change the image of pushable images (only works if the image is both pushable and hoverable!!)
	$(".imgPushable").mousedown( function() {
			var hoverImg = PushImgOf($(this).attr("src"));
			$(this).attr("src", hoverImg);
	});
	$(".imgPushable").mouseup( function() {
			var normalImg = HoverImgOf($(this).attr("src"));
			$(this).attr("src", normalImg);
	});
});

// This function is called after page has finished loading
$(window).bind('load', function() {
  // Preload hover and push images
  $(".imgHoverable").each(function() {
    var img = $(document.createElement('img'));
    img.attr("src", HoverImgOf($(this).attr("src")));
  });
  $(".imgPushable").each(function() {
    var img = $(document.createElement('img'));
    img.attr("src", PushImgOf($(this).attr("src")));
  });
});

function HoverImgOf(filename)
{
  if (filename.indexOf("-hover.") > 0)
    return filename;

  var re = new RegExp("(.+?)(-down)?\\.(gif|png|jpg)", "g");
	return filename.replace(re, "$1-hover.$3");
}
function PushImgOf(filename)
{
  if (filename.indexOf("-down.") > 0)
    return filename;

  var re = new RegExp("(.+?)(-hover)?\\.(gif|png|jpg)", "g");
	return filename.replace(re, "$1-down.$3");
}
function NormalImgOf(filename)
{
	var re = new RegExp("(.+)-(hover|down)\\.(gif|png|jpg)", "g");
	return filename.replace(re, "$1.$3");
}


//===============================================================================
// Auto-replace transparent PNG with transparent GIF for IE6
// Usage:
//   Have both .png and .gif versions with same filename, eg. submit.png, submit.gif
// In the .html or .aspx:
//   <img class="transparentPNG" src="<%AppPath%>images/submit.png"/>
//===============================================================================
$(document).ready(function() {
  var badBrowser = (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");
  if (badBrowser) {
    $(".transparentPNG").each(function() {
      this.src = ReplaceFileExtension(this.src, ".gif");
    });
  }
});

function ReplaceFileExtension(filename, newExt) {
  var re = new RegExp("(.+)\\.\\w+", "g");
  return filename.replace(re, "$1"+newExt);
}


// Usage: <input type="text" onkeypress="return InputDigitsOnly(event)" />
// eg. for postcode
function InputDigitsOnly(e) {
  var keycode = e.charCode ? e.charCode : e.keyCode;
  if (keycode == 8 || //backspace
      keycode == 37 || //left
      keycode == 39 || //right
      //keycode == 46 || //. (point)
      (keycode >= 48 && keycode <= 57)) {
    return true;
  }
  return false;
}

// Usage: your_string.Trim()
String.prototype.Trim = function() {
  return this.replace(/^\s*/, "").replace(/\s*$/, "");
}


/************ MELBOURNE PARTY EMPORIUM **************/

function txtSearch_KeyPress(e) {
    if ((e && e.which == 13) || (window.event && window.event.keyCode == 13))
        btnSearch_Click();
}
function btnSearch_Click() {
    window.location = '/cart.php?m=search_results&search=' + $('#txtSearch').val();    
}

// Hacks for Melbourne Party Emporium
$(document).ready(function() {

  /***** CATEGORY WIDGET (left menu) *****/
  
  // $('#widgetCategories .body > ul > li > a').each(function(){
    // var categImgFilename = 'categ-' + $(this).text() + '.png';
    // $(this).parent().css('background-image', 'url('+MERCHANT_URL+'images/'+categImgFilename+')');
  // });
  
  $('#dcatMenu > ul > li > a').each(function(){
    var categImgFilename = 'categ-' + $(this).text() + '.png';
    $(this).parent().css('background-image', 'url("'+MERCHANT_URL+'images/'+categImgFilename+'")');
  });
  
  // $('#widgetCategories .body > ul > li > a[href*="Balloon"]').parent().css('background-image', 'url('+MERCHANT_URL+'images/categ-balloon.png)');
  // $('#widgetCategories .body > ul > li > a[href*="Mask"]').parent().css('background-image', 'url('+MERCHANT_URL+'images/categ-mask.png)');
  // $('#widgetCategories .body > ul > li > a[href*="Hats"]').parent().css('background-image', 'url('+MERCHANT_URL+'images/categ-hats.png)');
  // $('#widgetCategories .body > ul > li > a[href*="Halloween"]').parent().css('background-image', 'url('+MERCHANT_URL+'images/categ-halloween.png)');
  // $('#widgetCategories .body > ul > li > a[href*="Valentine"]').parent().css('background-image', 'url('+MERCHANT_URL+'images/categ-valentine.png)');
  // $('#widgetCategories .body > ul > li > a[href*="Hen-s"]').parent().css('background-image', 'url('+MERCHANT_URL+'images/categ-hens.png)');
  // $('#widgetCategories .body > ul > li > a[href*="Pi-atas"]').parent().css('background-image', 'url('+MERCHANT_URL+'images/categ-pinata.png)');
  // $('#widgetCategories .body > ul > li > a[href*="Tableware"]').parent().css('background-image', 'url('+MERCHANT_URL+'images/categ-tableware.png)');
  // $('#widgetCategories .body > ul > li > a[href*="Christmas"]').parent().css('background-image', 'url('+MERCHANT_URL+'images/categ-xmas.png)');
  
  // $('#dcatMenu > ul > li > a[href*="Balloon"]').parent().css('background-image', 'url('+MERCHANT_URL+'images/categ-balloon.png)');
  // $('#dcatMenu > ul > li > a[href*="Mask"]').parent().css('background-image', 'url('+MERCHANT_URL+'images/categ-mask.png)');
  // $('#dcatMenu > ul > li > a[href*="Hats"]').parent().css('background-image', 'url('+MERCHANT_URL+'images/categ-hats.png)');
  // $('#dcatMenu > ul > li > a[href*="Halloween"]').parent().css('background-image', 'url('+MERCHANT_URL+'images/categ-halloween.png)');
  // $('#dcatMenu > ul > li > a[href*="Valentine"]').parent().css('background-image', 'url('+MERCHANT_URL+'images/categ-valentine.png)');
  // $('#dcatMenu > ul > li > a[href*="Hen-s"]').parent().css('background-image', 'url('+MERCHANT_URL+'images/categ-hens.png)');
  // $('#dcatMenu > ul > li > a[href*="Pi-atas"]').parent().css('background-image', 'url('+MERCHANT_URL+'images/categ-pinata.png)');
  // $('#dcatMenu > ul > li > a[href*="Tableware"]').parent().css('background-image', 'url('+MERCHANT_URL+'images/categ-tableware.png)');
  // $('#dcatMenu > ul > li > a[href*="Christmas"]').parent().css('background-image', 'url('+MERCHANT_URL+'images/categ-xmas.png)');

  // Balloon Chart (special menu item)
  $('#dcatMenu > ul > li > a[href*="Balloon"]').next().prepend("<li class='nosubcats'><a href='"+MERCHANT_URL+"Balloon-Chart-15.html'>Balloon Chart</a></li>");
  
  
  /***** HOME *****/
  // Add a label for Newsletter Subscribe
  $('#widgetSubscribe .body').prepend('<div class="label">Receive specials &amp; more</div>');
  
  // Add a blank widget-image on the right
  //$('#content-right').append('<img class="widgetBlank" src="/images/widget-blank.png"/>');
  
  // Move footer out of container
  // var footer = $('#footer').html();
  // $('#container #footer').remove();
  // $('body').append('<div id="footer">'+ footer +'</div>');
  $('#container #footer').appendTo('body');
  
  // Delete labels where we use background images as headings
  $('#widgetCategories .top').html('');
  $('#widgetSidebarDetailed .top a').html('&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;');
  $('#widgetSubscribe .top').html('');
  $('#widgetSubscribe .button a').html('&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;');
  $('#widgetFeatured .top').html('');
  $('#widgetSpecials .top').html('');
  $('#widgetTopSellers .top').html('');
  
  // Fix Specials / Top Sellers multiple items
  $('#content-middle .ccWidgetProduct .body .photo').each(function() {
    $('<div style="clear:both"></div>').insertBefore(this);
  });
  
  // Change Shopping Cart text
  $('#widgetSidebarDetailed td.price').prev().html('Total&nbsp;');
  $('#widgetSidebarDetailed input[value="Update Cart"]').val('Update');
  
  
  /***** PRODUCT DETAILS *****/
  // Add Review fix
  $('#productReviewsAdd > .body').append('<div style="clear:both"></div>');
});
