﻿jQuery(function($) {
    top_menu();
	new TopMenu();
    baner();
    login();
    search();
    help_tooltip();
	learnmore();

	function learnmore() {
		var hide = $('[moreinfobox="hide"]');
		var box = $('[moreinfobox="from"]');
		$('[moreinfobox="to"]').replaceWith(box);
		$("[learnmore1='show']").click(function(e) {
			e.stopPropagation();
			e.preventDefault();
			box.show();
			hide.hide();
		});
		$("[learnmore1='hide']").click(function(e) {
			e.stopPropagation();
			e.preventDefault();
			box.hide();
			hide.show();
		});		
	}
	
	function TopMenu() {
		ShowHideTopmenuBoxes = $.noop;

		var menu = $("#topmenubox");
		var opened_menu = undefined;

		function submenu(name) { return $("#" + name.replace("topmenu", "topmenubox")); };

		function close() { 
			menu.hide(0);
			$(".topmenulist [hover]").each(function(i, e) {
				e.className = e.className.replace("hovered", "");
			});
			opened_menu = undefined; 
		}

		$("#topmenubox, .topmenu").mouseout(function(e) {
			var r = $(e.relatedTarget);
			if(r.size() !=0 && r.parents(".topmenu").size() == 0)  close();
		});

		$('#topmenusearch, #topmenulogin, #topmenulanguages').mouseover(function(e) {
			topmeubox(submenu(this.id), this);
		});

		function topmeubox(elm, e) {
			if(opened_menu == elm[0].id) return;

			close();
			opened_menu = elm[0].id;
			elm.show().siblings().hide();
			menu.show();

			$("[hover]", e).each(function(i, e) {
				e.className = e.className + "hovered";
			});
		}
	} 

    function top_menu() {
        if(window.ddsmoothmenu !== undefined && $("#smoothmenu1").length != 0) {
			ddsmoothmenu.init({
				mainmenuid: "smoothmenu1", //menu DIV id
				orientation: 'h', //Horizontal or vertical menu: Set to "h" or "v"
				classname: 'ddsmoothmenu', //class added to menu's outer DIV
				//customtheme: ["#1c5a80", "#18374a"],
				contentsource: "markup" //"markup" or ["container_id", "path_to_menu_file"]
			});
			ddsmoothmenu.transition = { overtime: 100, outtime: 100 };
		}
    }

    function login() {
        var form = $("#form_login");
        function ssl() {
            var secure = $(".secure");
            secure.click(function() {
                var protocol = $(".securecheckbox").toggleClass("checkboxon").hasClass("checkboxon") ? "https" : "http";
                form.attr("action", form.attr("action").replace(/http\w*/g, protocol));
            });
            secure.click();
        }

        ssl();
        form.keydown(function(e) {
            if (e.keyCode == 13) {
                form.submit();
            }
        });
		$("[name='login']", form).click(function() { form.submit(); });
    }

    function baner() {
        var bannerquantity = 2;
        var bannerbox = $(".banner .container");
        RotateBanners(0);

        function RotateBanners(index) {
            bannerbox.addClass('slide' + index);
            bannerbox.fadeIn(1500);
            bannerbox.fadeOut(1200, function() { UppendBannerIndex(index); });
        }


        function UppendBannerIndex(index) {
            bannerbox.removeClass('slide' + index);
            if (index < bannerquantity) {
                RotateBanners(index + 1);
            } else {
                RotateBanners(0);
            }
        }
    }

    function search() {
        var elm = $("#search_engine");
        var content = $(".content");
        var search = $("#search");
        var control;
        var btnok = $(".search .btnok");
        
        if ($("#search_engine").size() > 0) create();

        function create() {
            function on() {
                google.load('search', '1', {
                    language: 'en',
                    callback: function() {
						var drawOptions = new GdrawOptions();  
                        control = new google.search.CustomSearchControl('007237784365227830282:sxtm55newz0');
						drawOptions.setDrawMode(google.search.SearchControl.EXPAND_MODE_CLOSED);

                        //control.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);

                        control.draw(elm[0].id, drawOptions);
						$("input.gsc-input").css("background", "");

                        elm.hide();
                        //$("form.gsc-search-box").hide()
                    }
                });
            }

            $.getScript(location.protocol + '//www.google.com/jsapi', on);
        }


        search
            .click(function() {
                search.val("");
                search.unbind("click");
            })
			.keydown(function(event) {
			    if (event.keyCode == 13) {
			        btnok.click();
			        return false;
			    }
			});


        btnok.click(function() {
            control.execute(search.val());
            elm.show();
            content.hide();
        });
    }

    function help_tooltip() {
        var tooltip = $("#tooltip");
		var text = $("[name='text']", tooltip);
		var data = {};
		$("[help_tooltip]").hover(
			function() {
				var elm = $(this);
				var offset = elm.offset()
				var id = Number($(this).attr("help_tooltip"));
				show("loading...");
				tooltip.show();
								
				if(data[id] != null) {
					show(data[id]);
				}else{					
					function on(r) {
						data[id] = r.Description;
						show(data[id]);
					}
					(new Common.Connector("Handlers/JSonHandler.ashx/Help_Tooltip/")).Post("GetHelp", { ID: id }, on);
				}				
				
				function show(txt) {
					text.text(txt);
					tooltip.css({ left: offset.left-tooltip.width()/2+elm.width()/2, top: offset.top-tooltip.height() });
				}			
			},
			function() {
				tooltip.hide();
			}
		);
    }
})

