﻿$(document).ready(function(){
	initTargetBlanks();
	initSearch();
	initBubbleMenu();
	initTabsSub();
	initTabsSubCover();
	initProductCompare();
	initOrderList();
	initAccess();
	initAccessCard();
	initAuthForm();
	initBenefits();
        initBenefits2();
        initBenefits3();
        initBenefits4();
        initBenefits5();
	initHints();
	initMainMenu();
	initShowAllAccess();
	initAccordeon();
	$(".card_add_block .add a").click(
		function(){
			//$(".card_add_block_popup").toggle();
			return false;
		}
	);
})

window.onload = function(){DrawCompare();};

function trim(value){
        val = value.replace(/^([\s]*)/g,"");
		val = val.replace(/([\s]*)$/g,"");
        return val;
    };
	
initTargetBlanks = function(){
	$(".target_blank").click(function(){
		window.open($(this).attr("href"));
		return false;
	})
}

initSearch = function(){
	/*
	$(".search_form input.text").focus(function(){
		if ($(this).attr("value") == $(this).attr("title")) {$(this).attr("value", "");}
	});
	$(".search_form input.text").blur(function(){
		if ($(this).attr("value") == "") {$(this).attr("value", $(this).attr("title"));}
	});
	*/
	$("input.text").focus(function(){
		if ($(this).attr("value") == $(this).attr("title")) {
			$(this).attr("value", "");
		}
		$(this).addClass("input_focus");
	});
	$("input.text").blur(function(){
		if (trim($(this).attr("value")) == "") {
			$(this).attr("value", $(this).attr("title"));
			$(this).removeClass("input_focus");
		}
	});
	$("textarea").focus(function(){
		if ($(this).attr("value") == $(this).attr("title")) {
			$(this).attr("value", "");
		}
		$(this).addClass("input_focus");
	});
	$("textarea").blur(function(){
		if (trim($(this).attr("value")) == "") {
			$(this).attr("value", $(this).attr("title"));
			$(this).removeClass("input_focus");
		}
	});
}

initBubbleMenu = function(){
	var subIndex = 0;
	$(".card_info_block > .item").hide();
	//$(".card_info_block .item:first").show();
	$(".card_info_block > .item").eq(0).show();
	$(".bubble_tabs li").click(function(){
		if (!$(this).hasClass("active")){
			$(".bubble_tabs li").removeClass("active");
			$(this).addClass("active");
		}
		if ($(this).parent("ul").hasClass("contacts_tabs")){
			subIndex = $(".bubble_tabs li").index($(this));
			$(".drive_map > .item").css("z-index", 5);
			$(".drive_map > .item").eq(subIndex).css("z-index", 10);
		}
		if ($(this).parent("ul").hasClass("card_tabs")){
			subIndex = $(".bubble_tabs li").index($(this));
			$(".card_info_block > .item").hide();
			$(".card_info_block > .item").eq(subIndex).show();
		}
	})
}

initTabsSub = function(){
	var subIndex = 0;
	$(".sub_item").hide();
	//$(".sub_item:first").show();
	$(".sub_item").eq(0).show();
	$(".tabs_sub li").click(
		function(){
			if (!$(this).hasClass("active")){
				$(".tabs_sub li").removeClass("active");
				$(this).addClass("active");
			}
			subIndex = $(".tabs_sub li").index($(this));
			$(".sub_item").hide();
			$(".sub_item").eq(subIndex).show();
		}
	);
}
initTabsSubCover = function(){
	$(".sub_item .tabs_sub_content").hide();
	$(".sub_item .tabs_sub_title").click(
		function(){
			if ($(this).hasClass("active")){
				$(this).parents(".sub_item").find(".show_all_acess").show();
				$(this).parents(".sub_item").find(".hide_all_acess").hide();
				$(this).parents(".sub_item").find(".show_all_acess").removeClass("active");
			}
			$(this).toggleClass("active");
			$(this).next(".tabs_sub_content").slideToggle();
		}
	);
}

initProductCompare = function(){
	var rel;
	var w = 200;
	var CurPos = 0;
	var num_columns = $(".compare_list_outer table .topper td").size()-1;

    // нужно для скрола
    var sc_object = $(".scrollObject"); // весь контент
    var sc_content = $(".scrollContent"); // видимая часть
    var sc_bar = $("#scroll_bar"); // контейнер для кнопки скрола
    var sc_button = $("#scroll_button"); // кнопка скрола
    // шаг ползунка при движении
    var deltaButton = sc_bar.width() / (sc_object.width() - 30) * 200;
    var CurButtonPos = 2;
	
	$(".compare_caption .header span.accord").click(function(){
		rel = $(this).attr("rel");
		if ($(this).hasClass("opened")){
			$(this).removeClass("opened");
			$(this).addClass("closed");
			$(".compare_caption tr[rel="+rel+"]").hide();
			$(".compare_list tr[rel="+rel+"]").hide();
				$("#show_all").show();
				$("#close_all").hide();
		}else{
			$(this).removeClass("closed");
			$(this).addClass("opened");
			$(".compare_caption tr[rel="+rel+"]").show();
			$(".compare_list tr[rel="+rel+"]").show();
			if ($(".compare_caption .header span.closed").size() == 0) {
				$("#show_all").hide();
				$("#close_all").show();
			}
		}	
	});
	
	$("#scroll_right").click(function(){
		//if (CurPos < 0) {$("#scroll_left").show();};
		$("#scroll_left").removeClass("disabled");
		if (CurPos > ((-1)*(num_columns-4)*w)){
            CurButtonPos = CurButtonPos + deltaButton;
			CurPos = CurPos-w;
		}
		if (CurPos < ((-1)*(num_columns-4)*w)){
			return false;
		}	
		$(".compare_list table").animate({left: (CurPos)+"px"}, {duration: 500, queue: false});
        $("#scroll_button").animate({left: (CurButtonPos)+"px"}, {duration:500, queue: false});
	});
	
	$("#scroll_left").click(function(){
		//if (CurPos <= ((-1)*(num_columns-4*w))) {$("#scroll_right").show();};
		$("#scroll_right").removeClass("disabled");
		if (CurPos < 0){
			CurPos = CurPos+w;
            CurButtonPos = CurButtonPos - deltaButton;
		}
		if (CurPos > 0){
			return false;
		}
		$(".compare_list table").animate({left: (CurPos)+"px"}, {duration: 500, queue: false});
        $("#scroll_button").animate({left: (CurButtonPos)+"px"}, {duration:500, queue: false});
	});
	
	$("#close_all").click(function(){
		$(".compare_list_outer table tr[rel]").hide();
		$(".compare_list_outer table tr.header span.accord").removeClass("opened");
		$(".compare_list_outer table tr.header span.accord").addClass("closed");
		$(this).hide();
		$("#show_all").show();
	})
	
	$("#show_all").click(function(){
		$(".compare_list_outer table tr[rel]").show();
		$(".compare_list_outer table tr.header span.accord").addClass("opened");
		$(".compare_list_outer table tr.header span.accord").removeClass("closed");
		$(this).hide();
		$("#close_all").show();
	})
	
	$("#param_show_diff").click(function(){
		$(".compare_caption tr:not(.header):not(.topper):not(.bordered) td:not(:has(strong))").hide();
		$(".compare_list tr:not(.header):not(.topper):not(.bordered) td:not(:has(strong))").hide();
	})
	$("#param_show_all").click(function(){
		$(".compare_caption tr:not(.header):not(.topper) td:not(:has(strong))").show();
		$(".compare_list tr:not(.header):not(.topper):not(.bordered) td:not(:has(strong))").show();
	})
}

function DrawCompare(){
	var temp = 0;
	var c_height;
	var l_height;
	var index;
	var item_width = 200;
	var num_columns = $(".compare_list_outer table .topper td").size()-1;

	if (num_columns > 4) {
		$("#scroll_right").show();
		$("#scroll_left").show();
	}
	
	$(".compare_list table").css({width: item_width * num_columns - 30+"px"});
	
	$(".compare_list tr").each(function(){
		l_height = $(this).height();
		index = $(".compare_list tr").index(this);
		c_height = $(".compare_caption tr").eq(index).height();

		if (l_height > c_height){
			$(".compare_caption tr").eq(index).find("td").css({height: l_height+"px"});
		}else{
			$(".compare_list tr").eq(index).find("td").css({height: c_height+"px"});
		}
	});
}

initOrderList = function(){
	$(".order_list .order_number h2>span").click(function(){
		$(this).parents(".item").toggleClass("opened");
	})
}

initAccess = function(){
	$(".access_block .head_block a").click(function(){
		$(this).toggleClass("closed");
		$(this).parents(".item_header").next(".model_list_block").toggle("slow");
	})
	
	$(".access_block .item_header img").click(function(){
		$(this).parents(".item_header").find("a").toggleClass("closed");
		$(this).parents(".item_header").next(".model_list_block").toggle("slow");
	})
	
	var anchor = window.location.hash;
	anchor = anchor.substr(1, anchor.length);
	
	//alert(anchor);
	
	if (anchor.length > 0) {
		$(".model_list_block").hide();
		$(".access_block a[name="+anchor+"]").next(".item").children(".model_list_block").show();
		$(".access_block a[name="+anchor+"]").next(".item").children(".head_block").find("p a:first").addClass("closed");
	}
	
	$(".left_column .sections_block ul li ul li a").click(function(){
		$(".model_list_block").hide();
		anchor = $(this).attr("href");
		anchor = anchor.substr(1, anchor.length);
		$(".access_block a[name="+anchor+"]").next(".item").children(".model_list_block").show();
		$(".access_block a[name="+anchor+"]").next(".item").children(".head_block").find("p a:first").addClass("closed");
	})
}

initAccessCard = function(){
	/*$("#acess_tab .new_list_block").eq(0).show();
	$("#acess_tab h2").eq(0).addClass("opened");*/
	$("#acess_tab h2").click(function(){
		$(this).toggleClass("opened");
		$(this).next(".new_list_block").toggle();
	})
};

initAuthForm = function(){
	$("#auth_links ul li.first a").click(function(){$("#auth_form").fadeIn();});
	$("#ClosePopup").click(function(){$("#auth_form").fadeOut(); $("#ForgotPasswordForm").hide();});
	$("#getForgotPasswordForm").click(function(){$("#ForgotPasswordForm").toggle();});
}

initBenefits = function(){
	$("#care_link").click(function(){
		if ($(this).hasClass("opened")) {
			$(this).removeClass("opened");
			$("#our_benefits").fadeOut();
		}else{
			$(this).addClass("opened");
			$("#our_benefits").fadeIn();
		};
	})
}


initBenefits2 = function(){
	$("#care_link2").click(function(){
		if ($(this).hasClass("opened")) {
			$(this).removeClass("opened");
			$("#our_benefits2").fadeOut();
		}else{
			$(this).addClass("opened");
			$("#our_benefits2").fadeIn();
		};
	})
}


initBenefits3 = function(){
	$("#care_link3").click(function(){
		if ($(this).hasClass("opened")) {
			$(this).removeClass("opened");
			$("#our_benefits3").fadeOut();
		}else{
			$(this).addClass("opened");
			$("#our_benefits3").fadeIn();
		};
	})
}


initBenefits4 = function(){
	$("#care_link4").click(function(){
		if ($(this).hasClass("opened")) {
			$(this).removeClass("opened");
			$("#our_benefits4").fadeOut();
		}else{
			$(this).addClass("opened");
			$("#our_benefits4").fadeIn();
		};
	})
}

initBenefits5 = function(){
	$("#care_link5").click(function(){
		if ($(this).hasClass("opened")) {
			$(this).removeClass("opened");
			$("#our_benefits5").fadeOut();
		}else{
			$(this).addClass("opened");
			$("#our_benefits5").fadeIn();
		};
	})
}

initHints = function(){
	$(".hint_outer img.question").click(function(){
		$(".hint_outer .hint").hide();
		$(this).next(".hint").css("display","block");
	});
	$(".del").click(function(){
		$(this).parents("span.hint").css("display","none");
	})
}

initMainMenu = function(){
	$("#main_menu td:has(a)").click(function(){
		window.location = $(this).find("a").eq(0).attr("href");
	})
}

initShowAllAccess = function(){
		$(".show_all_acess").click(function(){
			$(this).hide();
			$(this).parents(".sub_item").find(".hide_all_acess").show();
			$(this).parents(".sub_item").find(".tabs_sub_content").show();
			$(this).parents(".sub_item").find(".tabs_sub_title").addClass("active");
		});
		$(".hide_all_acess").click(function(){
			$(this).hide();
			$(this).parents(".sub_item").find(".show_all_acess").show();
			$(this).parents(".sub_item").find(".tabs_sub_content").hide();
			$(this).parents(".sub_item").find(".tabs_sub_title").removeClass("active");
		});
	};
	
initAccordeon = function(){
	$(".accordeon_link").click(function(){
		$("#"+$(this).attr("rel")).toggle();
		})
}
