// Javascript for Stores Menu Home (Rakshit Thakker)

/**
 * Equal Heights Plugin
 * Equalize the heights of elements. Great for columns or any elements
 * that need to be the same size (floats, etc).
 * 
 * Version 1.0
 * Updated 12/10/2008
 *
 * Copyright (c) 2008 Rob Glazebrook (cssnewbie.com) 
 *
 * Usage: $(object).equalHeights([minHeight], [maxHeight]);
 * 
 * Example 1: $(".cols").equalHeights(); Sets all columns to the same height.
 * Example 2: $(".cols").equalHeights(400); Sets all cols to at least 400px tall.
 * Example 3: $(".cols").equalHeights(100,300); Cols are at least 100 but no more
 * than 300 pixels tall. Elements with too much content will gain a scrollbar.
 * 
 */

(function($) {
	$.fn.equalHeights = function(minHeight, maxHeight) {
		tallest = (minHeight) ? minHeight : 0;
		this.each(function() {
			if($(this).height() > tallest) {
				tallest = $(this).height();
			}
		});
		if((maxHeight) && tallest > maxHeight) tallest = maxHeight;
		return this.each(function() {
			$(this).height(tallest).css("overflow","auto");
		});
	}
})(jQuery);


// Start : For Stores Menu

	var timeout         = 3;
	var closetimer		= 0;
	var ddmenuitem      = 0;


	function jsddm_open()
	{	
		jsddm_canceltimer();
		jsddm_close();
		
		if ($.browser.msie)
			$('select').css('visibility', 'visible');

		ddmenuitem = $(this).find('ul').eq(0).css('visibility', 'visible');
		ddmenuitem = $(this).find('ul').eq(0).css('display', 'block');
		
		
		ddmenuitem.css('background','#F3DAB5');
		ddmenuitem.removeClass('#TopMenuDropDown .jsddm li');
			
		if ($.browser.version.indexOf('1.9.1') == 0){
			ddmenuitem.css('top','22px'); 
		} else {
			ddmenuitem.css('top','40px');
		}
		
		ddmenuitem.addClass('temp');

		$(this).removeClass('regular_tab');
		$(this).addClass('nav_pad1');
		$(this).css('background','url(templates/talash/images/header/menu-ho.gif)');
		$(this).attr('id','selected_store');
		$('li.nav_pad1 a.nav_pad').css('color','#9e1711');


		var popup_div = $(this);
		var position = popup_div.position();
	
		var winH = $(window).height();
		var winW = $(window).width();

		try
		{
			if (($("ul#jsddm").find('li:first-child').find('ul').css('visibility')) != 'visible')
			{
				var old_menu_selection_width = $(this).width();
				var old_left = ddmenuitem.position().left;

				var listArray = $('ul#jsddm li');
				var selectedLI = listArray.index(listArray.find('.temp').parent());

				// For last list (menu) item
				if (($("ul#jsddm").find('li:last-child').find('ul').css('visibility')) == 'visible') {
					
					var lastMenuBlocks = $(this).find('.rolloverdiv').size();

				/*
					if(lastMenuBlocks>=4){
						ddmenuitem.css('width', (182*4+'px'));	
					}else{
						ddmenuitem.css('width', (175*lastMenuBlocks+'px'));
					}
				*/
					//ddmenuitem.css('left', (175*$(this).find('.rolloverdiv').size())+'px');
					//ddmenuitem.css('right', '0px');
					switch(lastMenuBlocks) {
						case 3:
						case 4:	
							//ddmenuitem.css('left',$(this).position().left-75);
							//break;
							
						case 5:
						case 6:
						//	ddmenuitem.css('left',$(this).position().left-150);
						//	break;
					
						default:
						//	ddmenuitem.css('left',$(this).position().left+1);
						//	break;
					}
					

					//$(this).find('.rolloverdiv').size();
					if (ddmenuitem.width() != 750) {
						//alert("yes");
						//ddmenuitem.css('left',(old_left + old_menu_selection_width)-ddmenuitem.width());
					}
				}
				else if ((($(this).find('.rolloverdiv').size()/2*170)+ddmenuitem.position().left) > winW) {
					//var dec = ($(this).find('.rolloverdiv').size() > 4)?85:0;
					//ddmenuitem.css('left', ((ddmenuitem.position().left))-(dec*2));
				}




				if (ddmenuitem.position().left < 0)

				{

					ddmenuitem.css('left', (old_left+(old_menu_selection_width/2)) - (ddmenuitem.width()/2.1));



					if (ddmenuitem.position().left < 0)

						ddmenuitem.css('left', 25*selectedLI);

				}



				if (ddmenuitem.position().left == $(this).position().left)

					//ddmenuitem.css('left',ddmenuitem.position().left - (selectedLI*15));
					ddmenuitem.css('left',ddmenuitem.position());

			}

			$("body").css("overflow-x", "hidden");
			//$(".rolloverdiv").equalHeights(200);
			//$(".rolloverdiv").css("overflow-x", "hidden");
		}

		catch(err) {}



	}



	function jsddm_close()

	{	if(ddmenuitem)

		{

			ddmenuitem.css('visibility', 'hidden');

			ddmenuitem.css('display', 'none');

			ddmenuitem.css('left', '');

			ddmenuitem.removeClass('temp');

			$('#selected_store').css('background','');

			$('#selected_store').removeClass('active_tab');

			$('#selected_store').addClass('regular_tab');
			$('#selected_store').removeClass('nav_pad1');
			
			$('li a.nav_pad').css('color','#FFFFFF');
			$('li.active a.nav_pad').css('color','#9e1711');

			$('#selected_store').attr('id','');
			
			$('select').css('visibility', 'visible');

		}

	}



	function jsddm_timer(){ 
		closetimer = window.setTimeout(jsddm_close, timeout);
	}



	function jsddm_canceltimer() {
		if(closetimer) {
			window.clearTimeout(closetimer);
			closetimer = null;
		}
	}



	$(document).ready(function()
	{
		$('#jsddm > li').bind('mouseover', jsddm_open);
		$('#jsddm > li').bind('mouseout',  jsddm_timer);
		$('#selected_store').mouseout(function(){$(this).addClass('selected_store');});
	});

	document.onclick = jsddm_close;