jQuery.fn.delay = function(time,func){
	return this.each(function(){
		setTimeout(func,time);
	});
};

$(document).ready(function() {

    onOpen();

    $("ul.sf-menu").superfish({
        animation: { height: 'show' },
        autoArrows: true,
        animation: { width: 'show' },
        dropShadows: false,
        speed: 'normal',
        onInit: onOpen,
        onShow: onShow,
        onHide: onHide,
        delay: 1

    }).find('li.current').showSuperfishUl();
    $('.sf-menu').bind("mouseleave", showCurrent);

    $('#n_1').bind("mouseenter", onParentHover);
    $('#n_2').bind("mouseenter", onParentHover);
    $('#n_3').bind("mouseenter", onParentHover);
    $('a').bind("click", checkLogin);

    function showCurrent() {
        if (/\/Inside.aspx\?CategoryID=[1-3]/.test(currentPage)) {
            showCategoryCurrent();
            return;
        }
        var currentNode = $("ul.sf-menu").find("a[href='" + currentPage + "']").parent();
        currentNode.addClass('current');
        currentNode.parents('li').addClass('current');
        var currentNodeA = currentNode.parents("li:last").children('a');
        $(this).delay(150, function() {
            currentNode.addClass("current").removeClass("sfHover");
            currentNode.parents("li").addClass("current").showSuperfishUl();

            var currentNodeId = currentNode.parents("li:last").children('a:first').attr('id');
            if (currentNodeId) {
                $("ul.sf-menu").css("background-image", "url(/i/milupamenu/" + currentNodeId + "_mbg.jpg)");
            }
            else {
                $("ul.sf-menu").css("background-image", "none");
            }
        });
    }

    function showCategoryCurrent() {
        clsName = '.n_hamileyim';
        clsIndex = 1;

        categoryId = currentPage.substring(currentPage.length - 1);

        switch (categoryId) {
            case "1":
                clsName = '.n_hamileyim';
                clsIndex = 1;
                break;
            case "2":
                clsName = '.n_0_12';
                clsIndex = 2;
                break;
            case "3":
                clsName = '.n_12_36';
                clsIndex = 3;
                break;
        }
        //console.log(clsName + " / " + clsIndex);
        $("ul.sf-menu").css("background-image", "url(/i/milupamenu/n_" + clsIndex + "_mbg.jpg)");
        $(clsName).addClass('sfHover');
        $(clsName).children("ul").eq(0).show().css('visibility', 'visible');
        //$(clsName).children("ul").eq(0).children('li').eq(0).addClass('current');
    }

    function clone(obj) {

        if (obj == null || typeof (obj) != 'object')

            return obj;

        var temp = {};

        for (var key in obj)

            temp[key] = clone(obj[key]);

        return temp;

    }


    function onOpen() {

        showCurrent();

        $('li.current > a').addClass('current-link');

        var i = 0;
        var abiler = $('ul.sf-menu li > a');
        var p;
        for (i = 0; i < abiler.length; i++) {
            obj = abiler.eq(i);
            txt = obj.text();

            if (txt.length > 30) {
                p = $(document.createElement('p'));
                p.attr('id', 'p' + i);
                p.addClass('false');
                p.text(txt.replace(/^\s*/, "").replace(/\s*$/, ""));


                obj.text(txt.substr(0, 30) + '...');
                obj.attr('rel', 'p' + i);
                obj.cluetip({ local: true, cursor: 'pointer', tracking: true, positionBy: 'fixed', dropShadow: false, topOffset: 50 });
            }
        }

        /*
        jQuery.each( $('li > a'),function(){
        if(this.text.length>30) {
        var ot = $(this).text;
        var p = $(document.createElement('p'));
        p.attr('id','p'+i);
        p.addClass('false');
        p.text(ot);
               
        substred = this.text.toString();
               
        $(this).text(substred.substr(0,30)+'...');
        $(this).attr('title',ot);   
        $(this).attr('rel','p'+i);   
        $(this).cluetip({local:true, cursor: 'pointer',tracking:true });
        i++;
        }
        });
        */

    }


    function onShow() {
    }

    $("ul.sf-menu").hover(
        function() {
            $('.flashBanner').css("z-index", 1);
        }, 
        function() {
            $('.flashBanner').css("z-index", 100);
        }
    );


    function onHide() {
        //$('.flashBanner').show();
        //$('.flashBanner').css("z-index", 100);
    }

    function onParentHover(e) {
        $(this).delay(150, function() {

            $("ul.sf-menu").css("background-image", "url(/i/milupamenu/" + e.target.id + "_mbg.jpg)");
            //$('.flashBanner').css("z-index", 1);
        })
    }

    function checkLogin(e) {

        if ($(e.target).attr('class') == 'memberOnly') {
            $.growlUI('Uyarı', 'Bu sayfayı görmek için üye olmalısınız');

            return false;
        } else {
            return true;
        }
    }
}); 
