/*
	File Name:		menu.js
	Description:	Yadig Drop-down and stuff..
	Version:		1
	Author:		Dmitry Korsunov
	E-mail:		dmitry.korsunov@gmail.com
	
	No Rights Reserved.
	But some credit would be appreciated.
*/

var gflag = 0;
var to = null;
var prev = 0;
var curID = 0;

function showMenu(m) {
	
	if (prev != 0) tmp = prev.substring(0,4);
	$("#"+tmp).removeClass('hover');
	
	if ($("#"+m).length == 0) {
		return true;
	}
	
	hideMenu(prev);
	prev = m;
	
	$("#"+curID).addClass('hover');
	$("#"+m).slideDown(150);
	
	gflag = 1;
	
}

function hideMenu(m) {

	if ($("#"+m).length == 0) {
		return true;
	}
	
	if (gflag==0) {
		
		$("#"+m).fadeOut(150);
		$("#"+curID).removeClass('hover');		
		clearTimeout(to);
		gflag = 0;
	}
	
}

function hideMenuTimed(m) {
	gflag = 0;
	to = setTimeout(function() {
		hideMenu(m);
	}, 50); 
}



$(document).ready(function(){

	$("a[id^='dl_']").hoverIntent(
	
		function() {
			curID = $(this).attr("id");
			curPos = $(this).position();
			tmp = curID + '_dd';

			$("#"+tmp).css("left", curPos.left+"px");
			showMenu(tmp);
			
		},	
		function() {}	
	);
	
	$("a[id^='dl_']").hover(
	
		function() {},
		
		function() {
			curID = $(this).attr("id");
			tmp = curID + '_dd';
			
			hideMenuTimed(tmp);
		}
	
	);
	
	$(".dd").hover(
	
		function() {					
			tmp = curID + '_dd';
			clearTimeout(to);				
		},
		
		function() {		
			tmp = curID + '_dd';
			hideMenuTimed(tmp);			
		}
	
	);
	
	$('.dd a').click(function() {
							  
		if(curID == "dl_1") {
			$("#inptCountry").attr("value", $(this).html()); 
			$("#dl_1").html($(this).html());
			tmp = $("#dl_1").parent().width(); 
			if (tmp >= 140) {
				$("#dl_1").css({ width: '135px' });
			} else {
				$("#dl_1").css({ width: 'auto' });
			}
			
			$('#dl_1'+'_dd'+' a').each(function(index) {
				$(this).removeClass('act');
			});
			$(this).addClass('act');
		}
		
		
		if(curID == "dl_2") {		  
			$("#inptCity").attr("value", $(this).html()); 
			$("#dl_2").html($(this).html());
			tmp = $("#dl_2").parent().width(); 
			if (tmp >= 115) {
				$("#dl_2").css({ width: '100px' });
			} else {
				$("#dl_2").css({ width: 'auto' });
			}
			
			$('#dl_2'+'_dd'+' a').each(function(index) {
				$(this).removeClass('act');
			});
			$(this).addClass('act');
		}
		
		if(curID == "dl_3") {
			$("#inptCategory").attr("value", $(this).html()); 
			$("#dl_3").html($(this).html());
			tmp = $("#dl_3").parent().width(); 
			if (tmp >= 150) {
				$("#dl_3").css({ width: '145px' });
			} else {
				$("#dl_3").css({ width: 'auto' });
			}
			
			$('#dl_3'+'_dd'+' a').each(function(index) {
				$(this).removeClass('act');
			});
			$(this).addClass('act');
		}
		
		if(curID == "dl_4") {
			$("#inptSubcategory").attr("value", $(this).html()); 
			$("#dl_4").html($(this).html());
			tmp = $("#dl_4").parent().width(); 
			if (tmp >= 155) {
				$("#dl_4").css({ width: '145px' });
			} else {
				$("#dl_4").css({ width: 'auto' });
			}
			
			$('#dl_4'+'_dd'+' a').each(function(index) {
				$(this).removeClass('act');
			});
			$(this).addClass('act');
		}
		
		if(curID == "dl_5") {
			$("#dl_5_txt").html($(this).html());
			$('#dl_5'+'_dd'+' a').each(function(index) {
				$(this).removeClass('act');
			});
			$(this).addClass('act');
		}
		
		if(curID == "dl_6") {
			$("#dl_6_txt").html($(this).html());
			$('#dl_6'+'_dd'+' a').each(function(index) {
				$(this).removeClass('act');
			});
			$(this).addClass('act');
		}
		
	});
	
	
	
	$('#keyword').click(function() {
		if ($(this).attr("value") == "By Keyword...") $(this).attr("value", "");
	});
	
	$("#keyword").blur(function() {
		if ($(this).attr("value") == "") $(this).attr("value", "By Keyword...");
    });
	
	
	$('#byCountry').click(function() {
		if ($(this).attr("value") == "By Country...") $(this).attr("value", "");
	});
	
	$("#byCountry").blur(function() {
		if ($(this).attr("value") == "") $(this).attr("value", "By Country...");
    }); 
	
	
	$('#byCity').click(function() {
		if ($(this).attr("value") == "By City...") $(this).attr("value", "");
	});
	
	$("#byCity").blur(function() {
		if ($(this).attr("value") == "") $(this).attr("value", "By City...");
    });
	
	
	$('.review .inpt input').click(function() {
		if ($(this).attr("value") == "Write review...") $(this).attr("value", "");
	});
	
	$(".review .inpt input").blur(function() {
		if ($(this).attr("value") == "") $(this).attr("value", "Write review...");
    }); 
	
	
	
	$(".tooltip a").hoverIntent(function() {
		$(this).next("em").stop(true, true).animate({opacity: "show", top: "-20"}, "slow");
	}, function() {
		$(this).next("em").animate({opacity: "hide", top: "-40"}, "fast");
	});
		
		
	/* --- Random Header --- */		
	var randNumb=Math.floor(Math.random()*3+1);	
	$("div[class^='bg']").attr('class', 'bg'+randNumb);
	
	
	/* --- Rating Tooltips --- */
	$(".tooltip2 a").hoverIntent(function() {
		$(this).next("em").css("left", $(this).position().left+"px");
		$(this).next("em").stop(true, true).animate({opacity: "show", top: "-26"}, "slow");
		//$(this).next("em").show();
	}, function() {
		$(this).next("em").css("left", $(this).position().left+"px");
		$(this).next("em").animate({opacity: "hide", top: "-36"}, "fast");
		//$(this).next("em").hide();
	});
	
	
	/* --- Search Field's Price Range --- */
	var selPriceRange;
	
	$("#priceRange a").hover(function() {
		$(this).siblings().removeClass("act");
		$(this).prevAll("a").addClass("hover");
	}, function() {
		$(this).prevAll("a").removeClass("hover");
		restoreRange();
	});
	
	$("#priceRange a").click(function() {
		$("#inptPriceRange").attr("value", $(this).next("em").children().html());
		$(this).addClass("act");
		$(this).prevAll("a").addClass("act");
		selPriceRange = $(this);
	});
	
	function restoreRange() {
		$(selPriceRange).addClass("act");
		$(selPriceRange).prevAll("a").addClass("act");
	}

});