	/*
	* Author: Alex Baskov, 2009
	* NOTE: make sure you include this script *after* jquery.js
	*
	* Feel free to include your custom logic if you want it to be on *every* page.
	*/

	$(document).ready(function () {
		//$("#m_actors_filter,#m_actors").hover(function () { showDropDown('actors'); });
		$("#m_actors_filter").click(function(e) { e.stopPropagation(); toggleDropDown('actors'); });
        $("#m_actors a").click(function() { if ($("#m_actors_dd").length && !$("#m_actors_dd").is(":visible")) { showDropDown('actors'); return false; }});
		//$("#m_voices_filter,#m_voices").hover(function () { showDropDown('voices'); });
		$("#m_voices_filter").click(function(e) { e.stopPropagation(); toggleDropDown('voices'); });
        $("#m_voices a").click(function() { if (!$("#m_voices_dd").is(":visible")) { showDropDown('voices'); return false; }});
		//$("#m_skills_filter,#m_skills").hover(function () { showDropDown('skills'); });
		$("#m_skills_filter").click(function(e) { e.stopPropagation(); toggleDropDown('skills'); });
        $("#m_skills a").click(function() { if ($("#m_skills_dd").length && !$("#m_skills_dd").is(":visible")) { showDropDown('skills'); return false; }});
		//$("#m_entertainers_filter,#m_entertainers").hover(function () { showDropDown('entertainers'); });
		$("#m_entertainers_filter").click(function (e) { e.stopPropagation(); toggleDropDown('entertainers'); });
        $("#m_entertainers a").click(function() { if (!$("#m_entertainers_dd").is(":visible")) { showDropDown('entertainers'); return false; }});
		$("div.dd_content,div.dd_bottom,#popupSubscribe").click(function (e) { e.stopPropagation(); });
		//$("#m_actors_dd,#m_voices_dd,#m_entertainers_dd,#m_skills_dd").hover(function() { }, function() {hideDropDowns(); });
		$(".tbox_holder, .mid_left, .box_actor_image").hover(function () { hideDropDowns(); });
		$(document).click(function(e) {
			//Hide all visible menus
			hideDropDowns();
			hidePopupSubscribe();
			if ($.browser.msie && $.browser.version == "7.0") {
				$(".box_abs.active").show(); 
			}
		}).keypress(function(e) {
			if(e.keyCode == 27) {
				hideDropDowns();
				hidePopupSubscribe();
			}
		});

		$("#actors_age_from").change(function(){
			$("#actors_age_to").empty();
			var age_from =  $("#actors_age_from").val();
			if (age_from > 0)
				$("#actors_age_to").append("<option value=''></option>");
			for (i = age_from; i<100; i++)   {
				$("#actors_age_to").append("<option value='" + i + "'>" + i + "</option>");
			}
		});
		
		$("#voices_age_from").change(function(){
			$("#voices_age_to").empty();
			var age_from =  $("#voices_age_from").val();
			if (age_from > 0)
				$("#voices_age_to").append("<option value=''></option>");
			for (i = age_from; i<100; i++)   {
				$("#voices_age_to").append("<option value='" + i + "'>" + i + "</option>");
			}
		});
		
		$("input[id^=voices_gender]").click(function(){
			$("input[id^=voices_gender]").attr('checked','');
			$(this).attr('checked','checked');
		})
		
		tlogin_validator = $("#form_tlogin").validate({
			rules: {
				josso_username: {
					required: true
				},
				josso_password: {
					required: true
				}
			},
			errorPlacement: function(error, element) {
				//None
			},
			success: function(label) {
				//No action
			},
			highlight: function(element, errorClass) {
				//No action
			},
			invalidHandler: function() {
				//No action
			}
		});
		
		// fixing DD layouting and zIndex for IE7. also checking whether we're on homepage
		//if ($("#his_text").length && $.browser.msie && $.browser.version == "7.0")
		//{
		//	fixZindexIE();
		//}
	});
		


	////
	//
	//
	function toggleDropDown(cat)
	{
		var cat = cat || null;
		if (!cat) return false;
		
		if ($.browser.msie && $.browser.version == "7.0") {
			if ($("#shortlist_holder").length > 0) {
				if (isShortlistVisible()) {
					toggleShortlist();
				}
			}
			$(".box_abs.active").hide();
		}
				
		hidePopupSubscribe();
		// here we're closing all dropdowns except the one which was clicked
		$("#top_nav li").each(function () {
			if ($(this).attr("id") != "m_" + cat + "_filter" && $(this).attr("id") != "m_" + cat)
			{
				if (location.href.indexOf($(this).attr("id").substr(3)) == -1)
					$(this).removeClass("active");
				$("#" + $(this).attr("id") + "_dd").hide(); // or use slideUp("fast")
			}
		});

		$("#m_" + cat + "_dd").slideToggle("fast");
		if (location.href.indexOf(cat) == -1) $("#m_" + cat).toggleClass("active");
		$("#m_" + cat + "_filter").toggleClass("active");
		
		return true;
	} // /toggleDropDown()
	
	function showDropDown(cat)
	{
		var cat = cat || null;
		if (!cat) return false;
		
		if ($.browser.msie && $.browser.version == "7.0") {
			if ($("#shortlist_holder").length > 0) {
				if (isShortlistVisible()) {
					toggleShortlist();
				}
			}
			$(".box_abs.active").hide();
		}
		
		hidePopupSubscribe();
		// here we're closing all dropdowns except the one which was clicked

		if (!($("#m_" + cat + "_dd").is(":visible"))) {
			hideDropDowns();
			$("#m_" + cat + "_dd").slideDown("fast");
			if (location.href.indexOf(cat) == -1) $("#m_" + cat).addClass("active");
			$("#m_" + cat + "_filter").addClass("active");
		}
		
		return true;
	} // /showDropDown()
	
	function hideDropDowns()
	{	
		$("#top_nav li").each(function () {
				if (location.href.indexOf($(this).attr("id").substr(3)) == -1)
					$(this).removeClass("active");
				$("#" + $(this).attr("id") + "_dd").slideUp("fast");
		});
	}
	function hidePopupSubscribe()
	{
		$("#popupSubscribe").hide();
		$("#linkSubscribe").removeClass("active");
	}
	
	////
	//
	//
	function goToPage(cat)
	{
		var cat = cat || null;
		if (!cat) return false;
		goUrl("/" + cat);		
		return true;
	} //
	
	function goUrl(url) {
		window.location = url;
    }
	
	function realValueOf(field) {
		if ($(field).val() != $(field).attr("title")) {
			return $(field).val();
		}
		return "";
	}

	function loginToApp(){
		var data = "josso_username=" + realValueOf("#josso_username") +
                   "&josso_password=" + realValueOf("#josso_password"); 
		$.ajax({
            type: "POST",
			// TODO change it to sso.at2casting.com
            url: "http://sso-staging.at2casting.com/josso/signon/login.do?josso_back_to=https://localhost/app/josso_security_check",
            data: data
        });
	}
