var ttClosable = false;
$(function(){
	
    if($.browser.msie && parseInt($.browser.version) == 6) {
        $("#header #main_menu").width($(window).width());
        $("#footer").css("top", $(window).height() - 22);   
    }
    	   
	var mxHeight = 0;	
	$(".columns").each(function() {
		if($(this).height() > mxHeight) {
			mxHeight = $(this).height();
		}
	}).height(mxHeight);
    
    $(".csbody").each(function(i, item) {
	    $(item).toggle(function() {
			$(item).children("ul").show();
	    }, function(/*e*/) {
            $(item).children("ul").hide();
        });
    });
	
	$(".csbody ul li").not(".head").click(function() {
		$(this).parents(".csbody").children(".cstext").text($(this).text());
        var val = $(this).children().attr("rel");
        $(this).parents(".csbody").children(".csval").text(val);
		$(this).parents(".csbody ul").hide();
        
        //selected item change
        $(this).trigger("csChange", [val]);
	}).hover(function() {
		$(this).css("background-color", "#003399").children("a").css("color", "#FFF");	
	}, function() {
		$(this).css("background-color", "#FFF").children("a").css("color", "#4C4C4C");	
	});
    
    $(".customselect .input[name=search], .customselect .input[name=mp_search]").click(function() {
        if($(this).val() == "Keresés...") {
            $(this).val("");
        }
    }).blur(function() {
        if($(this).val() == "") {
            $(this).val("Keresés...");
        }
    });
	
	
	/* Navigation menu control */
	$("#navigation_footer").click(function() {								   
		$("#navigation_list").slideUp(function() {
			$("#navigation_top").show();
            $("#navigation_header").css("border-bottom", "1px solid #7B7B7B");										   
		});					 
	}).hover(function() {
		$(this).css("background-position", "0px -12px");	
	}, function() {
		$(this).css("background-position", "0px 0px");	
	});
	
	$("#navigation_top").click(function() { 							
		$(this).hide();
		$("#navigation_header").css("border-bottom", "none");
		$("#navigation_list").slideDown();
	}).hover(function() {
		$(this).css("background-position", "0px -12px");	
	}, function() {
		$(this).css("background-position", "0px 0px");	
	});
	
	/* Topmenu control */
	var menuHoverable = false;
	var layerOpen = false;
	$(".mainmenu > .menuitem > a").not("#home, #config").click(function() {
		var li_a = $(this);
		li_a.next(".layer").show(1, function() {							  
			li_a.removeClass().addClass("openlayer");
			layerOpen = true;
			menuHoverable = true;
            $("#navigation_top").css("z-index", "1");
			$("#navigation_list").css("z-index", "9");
		});
		
	}).mouseover(function() {
		if(menuHoverable) {
			clearAll();
			var li_a = $(this);
			li_a.next(".layer").show(1, function() {							  
				li_a.removeClass().addClass("openlayer");
				layerOpen = true;
				$("#navigation_top").css("z-index", "1");
                $("#navigation_list").css("z-index", "9");
			});	
		}
	});
	
	function clearAll() {
		$("#navigation_top").css("z-index", "12");
        $("#navigation_list").css("z-index", "11");
		$(".mainmenu > .menuitem > a").not("#home, #config").each(function() {
			var li_a = $(this);
			li_a.next(".layer").hide(1, function() {							  
				li_a.removeClass("openlayer");	
				layerOpen = false;
			});
		});
	}
	
	$(document).click(function() {
        $(".csbody ul").hide();
		if(layerOpen) {
			clearAll();	
		}
		menuHoverable = false;
    });
    
    /* Content Column height fixes */
    /*var column1Height = $("#content .text_column1").offset().top + $("#content .text_column1").height();
    var column2Height = $("#content .text_column2").height(); 
    var column3Height = $("#content .text_column3").height(); 
    if(column1Height > column2Height && column1Height > column3Height) {
        $("#content .text_column3").height(column1Height - 120);
    }
    if(column2Height > column1Height && column2Height > column3Height) {
        $("#content .text_column3").height(column2Height - 120);
    }*/
    
    $(".c_possibilities img, #bImageContainer img").not(".lens").hover(function() {
        var idx = $(".c_possibilities img, #bImageContainer img").not(".lens").index($(this));
        if($("img.lens").eq(idx).attr("src").indexOf("lens") > -1) {
            $("img.lens").eq(idx).attr("src", baseUrl + "images/lens-h.gif");    
        } else {
            $("img.lens").eq(idx).attr("src", baseUrl + "images/close-h.gif");    
        }
    }, function() {
        var idx = $(".c_possibilities img, #bImageContainer img").not(".lens").index($(this));
        if($("img.lens").eq(idx).attr("src").indexOf("lens") > -1) {
            $("img.lens").eq(idx).attr("src", baseUrl + "images/lens.gif");    
        } else {
            $("img.lens").eq(idx).attr("src", baseUrl + "images/close.gif");    
        }
    });
    
    $(".faqdiv").hide().eq(0).show();
    $("a[rel=content_opener]").each(function(i, item) {
        if(i == 0 && $(".ddiv[rel=active]").length == 0) {
            $(this).removeClass().addClass("xrelated_link_active");    
        }
        $(this).click(function() {
            $("a[rel=content_opener]").removeClass().addClass("xrelated_link");
            $(this).removeClass().addClass("xrelated_link_active");
            $(".faqdiv").hide().eq(i).show();
        });    
    });
    
    if($(".ddiv[rel=active]").length == 0) {
        $(".ddiv").hide().eq(0).show();
    } else {
         $(".ddiv").hide();
         $(".ddiv[rel=active]").show();
    }
    $(".dfile").hide().eq(0).show();
    $("a[rel=content_opener]").each(function(i, item) {
        if(i == 0 && $(".ddiv[rel=active]").length == 0) {
            $(this).removeClass().addClass("xrelated_link_active");    
        }
        $(this).click(function() {
            $("a[rel=content_opener]").removeClass().addClass("xrelated_link");
            $(this).removeClass().addClass("xrelated_link_active");
            $(".ddiv").hide().eq(i).show();
            $(".dfile").hide().eq(i).show();
            $(".cscroll#news_content").show();
            $("#text_wrap").css("top", 0);
            $(".scrollbody_news_content").css("top", 0);
            //dragHandle(".scrollbody_news_content", 358, "#text_wrap", ".text_column2", 450, ".cscroll#news_content a", ".cscroll#news_content");
        });    
    });

    /** rider equipment hover handle **/
    $("#bImageContainer img").not(".lens").toggle(function() {
        var newSrc = $(this).attr("src").replace(/_medium/, "_large");
        $(this).attr("src", newSrc)/*.css({ width : 616, height : 462 }).parent("#bImageContainer").css({ width : 618, height : 480 })*/;
        $(".lens").attr("src", baseUrl + "images/close-h.gif");
    }, function() {
        var newSrc = $(this).attr("src").replace(/_large/, "_medium");
        $(this).attr("src", newSrc)/*.css({ width : 328, height : 246 }).parent("#bImageContainer").css({ width : 330, height : "auto"})*/;
        $(".lens").attr("src", baseUrl + "images/lens-h.gif");
    });
    
    $("#addInfoContent .tab").carousel({ 
        dispItems: 4,
        effect: "fade",
        prevBtn:'<div class="addInfoContentCounterBack"><img id="backImgProducts" src="'+baseUrl+'images/equipment_back.gif" /></div>',
        nextBtn:'<div class="addInfoContentCounterNext"><img id="nextImgPictures" src="'+baseUrl+'images/equipment_next.gif" /></div>'
    });
    
    $("#addInfoContent .tab").hide().eq(0).show();
    $(".addInfoTabItem").click(function() {
        var idx = $(".addInfoTabItem").index($(this));
        $("#addInfoContent .tab").hide().eq(idx).show();
        $(".addInfoTabItem").removeClass("aitActive").eq(idx).addClass("aitActive");
        $(".carousel-wrap").height($("#addInfoContent").height());
        $(".carousel-wrap ul li").css("height", "auto");
    });
    
    /** Rider equipment icon explanation box **/
    closeTt();
    $(".propertyIcon").mouseover(function(e) {
        $("#iconExplanation").hide();
        var imgClass = $(this).children().children().attr("class");
        if($("#iconExplanation:not(:visible)").length > 0) {
            $("#iconExplanation > div").each(function(i, item) {
                if($(item).hasClass(imgClass)) {
                    $(item).show();    
                } else {
                    $(item).hide();    
                }
            });
            $("#iconExplanation").css({
               left : e.pageX,
               top : e.pageY
            }).fadeIn(300);    
        }
        ttClosable = false;
    }).mouseout(function() {
        ttClosable = true;  
    });
            
});

function closeTt() {
    var ttTo = setTimeout("closeTt()", 800);
    if(ttClosable) {
        $("#iconExplanation").hide(300);
    }
}

function preview(img) {
    $("#big_image_layer").html('<img width="510" border="0" src="'+img+'" /><br /><br /><a class="xrelated_link" href="javascript:previewClose()">Bezárás</a><br /><br />').show();
}

function previewClose() {
    $("#big_image_layer").hide();
}

function fillData(type, column) {
    $.getJSON("index.php",
        { type : type, cmd : "gettechnicaldata" },
        function(data) {
            $(data).each(function(i, item) {
                if(item.key == "") {
                    //alapár
                    $("table.data tr:last td:eq(" + column + ")").text(item.val);     
                } else if(item.key == "color") {
                    //kép
                    $("#md_image" + column).attr("src", item.val);                    
                } else {
                    $("table.data tr td:contains('" + item.key + "')").parent().children(":eq(" + column + ")").text(item.val);                
                }
            });
        }
    );
}

function printDataSheet() {
    window.print();    
}     

function changeColor(nSrc) {
    $("#bImageContainer img").not(".lens").attr("src", baseUrl + "images/rider_equipment/" + nSrc);
}

$(window).load(function() {
	/** carousel height fix **/
    $(".carousel-wrap").height($("#addInfoContent").height());
});

