/**
 * @package		HillsFlorist
 * @version		2008-11-15
*/

/*	Global Configuration
----------------------------------------------- */
jQuery.noConflict();
var $j = jQuery;
var isIE6 = false;
var isSafari = false;

/*	Global Literal Object - Site-wide functions
----------------------------------------------- */
var Global = {

	/* Configuration */

	/* Utility: Browser Tests / Specific Fixes/Hooks */
	utilBrowserTests : function() {

		/* Checks for IE6 using object detection, then applies the fix for background flicker bug */
		if (typeof document.body.style.maxHeight == "undefined") { isIE6 = true; try { document.execCommand('BackgroundImageCache', false, true); } catch(e) { } }

		/* Checks for Safari and adds class of .safari to div#Container */
		if($j.browser.safari) { isSafari = true; $j("div#Container").addClass("safari"); }

		/* Checks for Mozilla and applies the Opacity fix for Mac */
/* ToDo: Platform detection Mac */
		if($j.browser.mozilla) { $j("body").css('opacity', '.9999'); }

	},

	/* Utility: Form Legends */
	utilFromLegends : function() {
		$j("legend.accessibility").hide();
		$j("legend.replace").each(function(){
			var jObj = $j(this);
			var cssClassesStr = jObj.attr("class").toString();
			var cssClasses = cssClassesStr.split("replace replace-");
			jObj.after("<"+ cssClasses[1] + " class=\"legend\">"+ jObj.html() +"</"+ cssClasses[1] +">").remove();
		});
	},

	/* Utility: External Links */
	utilExternalLinks : function() {
/* ToDo: Insert text inside anchor (this link opens in new window) */
		$j("a.external").each(function(){
			$j(this).click(function(){return !window.open($j(this).attr("href"));});
		});
	},

	/* Initialise Modals and bind Events */
	initModals : function() {

		/* Product Display Class Context */
		var cc = this;

		/* Show Modal, fadeIn and Hide Flash and Selects */
		var showModal = function(mdlID) {
			$j('embed, object, select', '#Content').addClass('jqmHide');
			mdlID.w.hide();
			mdlID.w.fadeIn();
		};

		/* Remove Modal, fadeOut and restore Flash and Selects */
		var removeModal = function (mdlID) {
			mdlID.w.fadeOut('800', function() {
				$j('embed, object, select', '#Content').removeClass('jqmHide');
				mdlID.o.remove();
			});
		};

	},

	/* Initialise */ 
	init : function() {
		/* Class Context */
		var cc = this;
		cc.utilBrowserTests();
		cc.initModals();
//		cc.utilFromLegends();
	}

};

$j.fn.sortOptions = function(ascending)
{
	var a = typeof(ascending) == "undefined" ? true : !!ascending;
	this.each(
		function()
		{
			if(this.nodeName.toLowerCase() != "select") return;
			// get options
			var o = this.options;
			// get number of options
			var oL = o.length;
			// create an array for sorting
			var sA = [];
			// loop through options, adding to sort array
			for(var i = 0; i<oL; i++)
			{
				sA[i] = {
					v: o[i].value,
					t: o[i].text
				}
			}
			// sort items in array
			sA.sort(
				function(o1, o2)
				{
					// option text is made lowercase for case insensitive sorting
					o1t = o1.t.toLowerCase(), o2t = o2.t.toLowerCase();
					// if options are the same, no sorting is needed
					if(o1t == o2t) return 0;
					if(a)
					{
						return o1t < o2t ? -1 : 1;
					}
					else
					{
						return o1t > o2t ? -1 : 1;
					}
				}
			);
			// change the options to match the sort array
			for(var i = 0; i<oL; i++)
			{
				o[i].text = sA[i].t;
				o[i].value = sA[i].v;
			}
		}
	);
	return this;
};



/*	DOM Ready events
----------------------------------------------- */
$j(document).ready(function() {

	/* Fire global functions */
	Global.init();

	if (isIE6) {
		$j('#Toolbar').css('zoom', '1');
	}

	if ($j(".category-weddings").size()) {
		$j('.item-wedding-carousel ul').jcarousel({scroll:6});

		$j('.item-wedding-carousel ul li a').click(function() {
			var jObjProductAnchor = $j(this);
			var jObjProductAnchorHref = jObjProductAnchor.attr('href');
			var jEOMdlWinProductImg = $j('<div class="jqmWindow" id="modal-product-view-image"><a href="#" class="jqmClose"><img alt="" src="' + jObjProductAnchorHref + '" /> <strong>Close Window</strong></a></div>');
			$j('body').append(jEOMdlWinProductImg);
			jEOMdlWinProductImg.jqm({ overlay: '70', onShow: Global.initModals.showModal, onHide: Global.initModals.removeModal }).jqmShow();
			return false;
		});
	}

	if ($j(".product-view").size()) {
		var jObjProductAnchor = $j('.product-image a');
		var jObjProductAnchorHref = jObjProductAnchor.attr('href');
		var jEOMdlWinProductImg = $j('<div class="jqmWindow" id="modal-product-view-image"><a href="#" class="jqmClose"><img alt="" src="' + jObjProductAnchorHref + '" /> <strong>Close Window</strong></a></div>');
		jObjProductAnchor.append('<br /> View larger product image.');
		$j('body').append(jEOMdlWinProductImg);
		jObjProductAnchor.click(function() {
			jEOMdlWinProductImg.jqm({ overlay: '70', onShow: Global.initModals.showModal, onHide: Global.initModals.removeModal }).jqmShow();
			return false;
		});
	}

	/* Fire local page initialize methods */
	if (typeof initPage == "function"){ initPage(); }

	/* Fire Global.utilExternalLinks() last so any external links added via DOM during local page initialization will be activated */
	Global.utilExternalLinks();

});


