//doc ready...00
$(function(){
	
    //IE specific
    if($.browser.msie) {
        $("ul#main li.active").css("opacity", "0.7");
        $(".propertybox:first").css("margin-top", "25px");
        $("ul#main li.active ul#sub li a:eq(0)").css("margin-left", "22px");
    }
    //IE8 specific
    if($.browser.msie && parseInt($.browser.version)==8) {
        $("#search button, #pr_search button").css("padding-top", "5px");
        $(".mainpage_box button, .mainpage_productbox button, .catalog_container button, .page_control button").css("padding-top", "2px");
    }
    //OPERA    
    if($.browser.opera) {
        $("#search button, #pr_search button").css("padding-top", "5px");
        $(".mainpage_box button, .mainpage_productbox button, .catalog_container button, .page_control button").css("padding-top", "2px");
        $("#search input#searchtext, #pr_search input#searchtext").css("padding", "0 0 0 5px");
    }
    //SAFARI e.g CHROME
    if($.browser.safari) {
        $("#search input#searchtext, #pr_search input#searchtext").css({padding:"0 2px 6px 5px", height:"22px"});
    }
    
    $("#pricons img[rel!=technical]").mouseover(function(ev) {                                 
        $(this).next().css({top:(ev.pageY-210),left:(ev.pageX-60)}).show();                                                                       
    }).mouseout(function(e) {
        $(this).next().hide();
    });
        
    if($("#products_content #left")[0]) {
        $("#mbline").css("top", $("#products_content #left")[0].clientHeight);
    }
    $("#mbline").css("height", $("#mainpage_footer").offset({scroll: false}).top-$("#products_content #left").height()-$("#pr_top").height()-116);
    
    $("#search button:eq(1), #pr_search button:eq(1)").click(function() {
        window.location.href="index.php?content=search&type=advanced";
    });
    subMenu();
    
    var bh_width = 0;
    $("#main_menu > ul > li").hover(function() {
        bh_width = $(this).width();
        if($(this).attr("id") != "menu_active") { 
            $(this).removeClass().addClass("subheader");
            $(this).width(bh_width-2);    
        }
    }, function() {
        if($(this).attr("id") != "menu_active") {
            $(this).removeClass();
            $(this).width(bh_width);    
        }
    });
    
    $("a.overlay").click(function() { 
        var dTitle = $(this).next().children("h3").text();
        var dContent = $(this).next().children("div.frame").html();
        var close = $(this).attr("rel");      
        $("#dialog").html('<img src="'+$(this).attr("href")+'" /><br /><div class="frame">'+dContent+'</div>').show().dialog({
            modal:true,
            resizable:false,
            width: 400,
            height:400,
            title:dTitle,
            overlay: { 
                opacity: 0.2, 
                background: "black" 
            },
            buttons: {
                "Bezár": function() { 
                    $(this).dialog("close");
                }
            }
        });
        $(".ui-dialog-buttonpane button").text(close);
        return false;
    });
    
    $("input[type=file]").change(function() {
        $(this).prev(".fileinput").val($(this).val());
    });
    
    $(".job_container button").toggle(function() {
        $(this).parent().next().slideDown(100, function() { fixHeight() });
    }, function() {
        $(this).parent().next().slideUp(100, function() { fixHeight() });
    });
    

    $('.refpics').cycle({ 
        fx:    "fade", 
        speed:  1000,
        pager: "#pcontrol",
        timeout: 5000,
        slideExpr: "img" 
     });
     
     $('#cyclecontainer').cycle({ 
        fx:    "fade", 
        speed:  3000,
        timeout: 8000,
        slideExpr: "img" 
     });
    
    $('a[@rel*=lightbox]').lightBox();
    
    $("#filtercontainer select").ajaxStart(function(){
       $(this).attr("readonly", "readonly");
       $("h3#found").text("Termékek szűkítése. Kérem várjon...");
    }).ajaxSuccess(function(){
       $(this).removeAttr("readonly");
    });

    
    $("#filtercontainer select").change(function() {
        var sourceIdx = $("#filtercontainer select").index(this);
        $("#resultcontent").hide();
        $.getJSON("index.php?content=search&type=advanced&func=adv",
            { 
                beepites : $("#filtercontainer select[name=beepitesimodok]").val(),
                fenyforras : $("#filtercontainer select[name=fenyforrastipusok]").val(),
                teljesitmeny : $("#filtercontainer select[name=teljesitmenyek]").val(),
                fenyforrasszam : $("#filtercontainer select[name=fenyforrasszamok]").val(),
                vilagitasimod : $("#filtercontainer select[name=vilagitasimodok]").val(),
                vilagitasifunkcio : $("#filtercontainer select[name=vilagitasifunkciok]").val(),
                fenyterelotipus : $("#filtercontainer select[name=fenyterelotipusok]").val()
            
            },
            function(data) {
                $.each(data, function(i, item) {
                    switch(i) {
                        /*case "beepites":
                            if($("#filtercontainer select").eq(sourceIdx).attr("name") != "beepitesimodok") {
                            $("#filtercontainer select[name=beepitesimodok] option").remove();
                            $.each(item, function(j, opt) {
                                $("#filtercontainer select[name=beepitesimodok]").append('<option value="'+j+'">'+opt+'</option>');
                            })
                            }
                            break;*/
                        case "fenyforras":
                            if(sourceIdx != 1 && $("#filtercontainer select[name=fenyforrastipusok]").val() == 0) { 
                                $("#filtercontainer select[name=fenyforrastipusok] option").remove();
                                $.each(item, function(j, opt) {
                                    $("#filtercontainer select[name=fenyforrastipusok]").append('<option value="'+j+'" '+(j==0 ? 'selected="selected"' : '')+' >'+opt+'</option>');
                                })
                            }
                            break;
                        case "teljesitmeny":
                            if(sourceIdx != 2 && $("#filtercontainer select[name=teljesitmenyek]").val() == 0) {
                                $("#filtercontainer select[name=teljesitmenyek] option").remove();
                                $.each(item, function(j, opt) {
                                    $("#filtercontainer select[name=teljesitmenyek]").append('<option value="'+j+'" '+(j==0 ? 'selected="selected"' : '')+' >'+opt+'</option>');
                                })
                            }
                            break;
                        case "fenyfszam":
                            if(sourceIdx != 3 && $("#filtercontainer select[name=fenyforrasszamok]").val() == 0) {
                                $("#filtercontainer select[name=fenyforrasszamok] option").remove();
                                $.each(item, function(j, opt) {
                                    $("#filtercontainer select[name=fenyforrasszamok]").append('<option value="'+j+'" '+(j==0 ? 'selected="selected"' : '')+' >'+opt+'</option>');
                                })
                            }
                            break;
                        case "vilagmod":
                            if(sourceIdx != 4 && $("#filtercontainer select[name=vilagitasimodok]").val() == 0) {
                                $("#filtercontainer select[name=vilagitasimodok] option").remove();
                                $.each(item, function(j, opt) {
                                    $("#filtercontainer select[name=vilagitasimodok]").append('<option value="'+j+'" '+(j==0 ? 'selected="selected"' : '')+' >'+opt+'</option>');
                                })
                            }
                            break;
                        case "vilagfunkc":
                            if(sourceIdx != 5 && $("#filtercontainer select[name=vilagitasifunkciok]").val() == 0) {
                                $("#filtercontainer select[name=vilagitasifunkciok] option").remove();
                                $.each(item, function(j, opt) {
                                    $("#filtercontainer select[name=vilagitasifunkciok]").append('<option value="'+j+'" '+(j==0 ? 'selected="selected"' : '')+' >'+opt+'</option>');
                                })
                            }
                            break;
                        case "fenyterelo":
                            if(sourceIdx != 6 && $("#filtercontainer select[name=fenyterelotipusok]").val() == 0) {
                                $("#filtercontainer select[name=fenyterelotipusok] option").remove();
                                $.each(item, function(j, opt) {
                                    $("#filtercontainer select[name=fenyterelotipusok]").append('<option value="'+j+'" '+(j==0 ? 'selected="selected"' : '')+' >'+opt+'</option>');
                                })
                            }
                            break;
                        case "talalatszam":
                            $("h3#found").text("Találatok száma: "+item+"db");
                            break;
                        case "html":
                            $("#resultcontent").html(item);
                            break;
                    }    
                });
                fixHeight(); 
            }
        );    
    });
    
    $("#filtercontainer button").toggle(function() {
        if($("#resultcontent").text().length > 0) {
            //console.log($("#resultcontent").text().length);
            $("#resultcontent").show();
            fixHeight();   
        } 
    }, function() {
        $("#resultcontent").hide();
        fixHeight();
    });
    
//doc ready end
});

$(window).load(function () {    
    fixHeight();
});

function fixHeight() {
    //BOTTOM POSITION FIX
    var reg = new RegExp("lang=(..)");
    if(window.location.search=="" || reg.test(window.location.search)) {
        $("#bottomline").css("top", $("#mainpage_footer").offset({scroll: false}).top-12);
    } else {
        $("#bottomline").css("top", $("#mainpage_footer").offset({scroll: false}).top);
    }

}
    
function signUp() {
    $.getJSON("index.php?content=signup",
    {email:$("input[name=newsletter_signup]").val()},
    function(data, textStatus) {
        $("#dialog").show().dialog({
            modal:true,
            resizable:false,
            width: 400,
            height:300,
            title:data[0].dialogTitle,
            overlay: { 
                opacity: 0.2, 
                background: "black" 
            },
            buttons: {
                "Bezár": function() { 
                    $(this).dialog("close");
                }
            }
        }).html(data[0].dialogText); 
        $(".ui-dialog-buttonpane button").text(data[0].closeText);   
    });
}    

var closeable = false;
$("ul.submenu").hide();

function subMenu() {
        
    $("#main_menu > ul > li > a").mouseover(function() {
        if($(this).next().length > 0) {
            $("ul.submenu").hide();
            //console.log($(this));
            $(this).parent().addClass("subheader");
            
            $(this).next().show();
            closeable = false;
        }
    }).mouseout(function() {
        closeable = true;
    });
    $("ul.submenu").mouseover(function() {
        closeable = false;        
    }).mouseout(function() {
        closeable = true;
    });
    $("#main_menu > ul > li").mouseover(function() {
        if($(this).children("ul").length > 0) {
            closeable = false;
        }
    }).mouseout(function() {
        closeable = true;
    });

}

function subMenuClose() { 
    to=setTimeout("subMenuClose()", 200);
    if(closeable) {
        $("ul.submenu").hide();
        /*$("#main_menu > ul > li").each(function() {
            if($(this).attr("id") != "menu_active") {
                $(this).removeClass("subheader");
            }
        });*/
    }
}
subMenuClose();

function langSwitch(lg) {
    window.location.href=window.location.href+'&lang='+lg;
}

function sendContent() {
    url = window.self.location.href;
    window.location.href = "mailto:?body="+escape(url);
}

function download(url) {
    window.open(url);
}

function printContent(url) {
    window.open("index.php?content=print&url="+escape(url)); 
}
                       
function gmapLoad() {
    /*if(GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("gmap"));
        map.setCenter(new GLatLng(47.620542, 19.161487), 15);
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());

    }*/
}

function showTechnicalPage(pid) {
    window.open("index.php?content=technical&pid="+pid, "", "scrollbars=1,width=600,height=800");
    //window.open("index.php?content=technical&pid="+pid, "Compass.hu Műszaki adatlap", "scrollbars=1,width=600");
}


/*function productFilter(val) {
    $.post("index.php?content=search&type=advanced&func=getfoundnum",
        {id:val},
        function(response) {
            $("#filtercontainer h3#found").html(response);        
        }
    );
}*/