/*
 *
 * @author: Alexander G.
 * Mail: fluder@gmail.com
 *
 * Menu Hiding
 *
 */

/**
 *
 * jQuery onready trigger
 *
 */
$(document).ready(function(){

    /**
     *
     * Flash Messanger
     *
     */
    $("#flash").hide();
    
    jQuery.fn.flashMessenger = function(status, messages) {
        if (status !== "success") {

            messages = 'Ошибка: ' + messages;
            $("#flash").attr("style", "color: red")                                        
        } else {
            $("#flash").attr("style", "color: black")
        }

        $("#flash").html(messages)
                   .slideDown("slow")
                   .delay(3000)
                   .slideUp("slow");
    }

    /*
     *
     * Middle slider
     *
     */
    big_block = $("[class=big-block-tab-1]").html();
    $(".brick-2").html(big_block);
    $("#hidden-slider").hide();

    i = 478;
    j = 2;

    interval_id = setInterval(function () {

        if (j == 6) {
            j = 1;
            i = 0;
        }
        
        big_block = $("[class=big-block-tab-" + j + "]").html();
        
        $(".brick-2").html(big_block);
        $(".brick-2").animate({backgroundPosition: '(-'+ i + 'px 0px)'});

        i = i + 478;
        j = ++j;
        

    }, 5000);

    k = 1;
    $(".box").live('click', function(){

        clearInterval(interval_id);
        tab_id = $(this).attr('class').replace(/\D/g,'');

        big_block = $("[class=big-block-tab-" + tab_id + "]").html();
        $(".brick-2").html(big_block);

        size = k*478;
        $(".brick-2").animate({backgroundPosition: '(-' + size + 'px 0px)'});
        k = ++k;
    });

    submenuOpened = false;
    lastHovered = false;
    
    /**
     *
     * Submenu slider
     *
     */
    $("#menu li").click(function(){
        var el = $(this).find("a");
        
        //$("#submenu").hide();
        
        if($(this).attr('class') == 'navi-without-submenu') {
            current_element_id = 'navi-without-submenu';
        } else {
            current_element_id = $(this).attr('id');
        }
        
        last_element_id = $("#last-element-id").attr('value');
        lastHovered = $(this).attr('id');

        switch(current_element_id) {
            case 'navi-without-submenu':
                el.attr("style", "border-bottom: 1px dotted darkgray; background: #e5eff7");
                $("#last-submenu-flag").attr('value', '1');
                break;
            default:
                select = $("[class=" + current_element_id + "]").clone().wrap("<ul></ul>").parent().html();
                $("#submenu").html(select);
             
                if(submenuOpened == true) {
                    el.hover(function(){
                        el.css({"background":"#e5eff7","border-bottom":"1px dotted darkgray"
                        });
                    }, function(){
                        el.css({"background":"white","border-bottom":"1px dotted darkgray"});
                    }); 
                    $("#submenu").slideUp()   
                    last_ul.find("a").attr("style", "");
                    submenuOpened = false;
                } else {
                    el.css({"background":"#e5eff7","border-bottom":"1px dotted #e5eff7"});
                    el.hover(function(){
                        el.css({"background":"#e5eff7",
                            "border-bottom":"1px dotted #e5eff7"
                        });
                    }, function(){
                         el.css({"background":"#e5eff7",
                            "border-bottom":"1px dotted #e5eff7"});
                    });

                    $("#submenu").slideDown();
                    submenuOpened = true;
                }          
                break;
        }

        if ((last_element_id != current_element_id) && (last_element_id != '')) {
            $("#" + last_element_id).find("a").css({
                "border-bottom" : "none", 
                "background"    : "white"}).hover(function(){
                $(this).css("background", "#e5eff7");
            }, function() {
                $(this).css("background", "white");
            });
            
        }

        $("#last-element-id").attr('value', current_element_id);

    
        last_submenu_flag = $("#last-submenu-flag").attr('value');

        if (last_submenu_flag == '1') {
            $(this).find("a").attr("style", "border-bottom: 1px dotted darkgray; background: white");
            $("#last-submenu-flag").attr('value', '');
        } else {
            //$(this).find("a").attr("style", "border-bottom: 1px dotted #e5eff7; background: #e5eff7");
            last_ul = $(this);
        }

    });
    
    
    $("#menu li").hover(function(){
        if(submenuOpened == true) {    
            
            var el = $(this).find("a");
            if($(this).attr("class") != 'navi-without-submenu') {
                $("#submenu").show();
                el.css({"background":"#e5eff7","border-bottom":"1px dotted #e5eff7"}).hover(function(){
                    el.css({"background":"#e5eff7","border-bottom":"1px dotted #e5eff7"});
                }, function(){
                    el.css({"background":"#e5eff7","border-bottom":"1px dotted #e5eff7"});
                });
            } else {
                $("#submenu").hide();
            }
            var current_element_id = $(this).attr('id');
            select = $("[class=" + current_element_id + "]")
                .clone()
                .wrap("<ul></ul>")
                .parent()
                .html();          
            $("#submenu").html(select);
            
            if((lastHovered != false) && (lastHovered != current_element_id)) {
                var elm = $("#" + lastHovered).find("a");
                elm.css({"background":"white","border-bottom":"1px dotted darkgray"});
            } 
            
            lastHovered = $(this).attr('id');
            window.clearInterval(menuTimer);
        } else {
            var el = $(this).find("a");
            el.css({"background":"#e5eff7","border-bottom":"1px dotted darkgray"}).hover(function(){
                el.css({"background":"#e5eff7","border-bottom":"1px dotted darkgray"});
            }, function(){
                el.css({"background":"white","border-bottom":"1px dotted darkgray"});
            });
        }
    }, function(){
        if(submenuOpened == true) {
            menuTimer = window.setInterval(function () {
               var elm = $("#" + lastHovered).find("a");
               elm.attr("style", "background: white;");

               $("#submenu").slideUp();
               submenuOpened = false;
               window.clearInterval(menuTimer);    
            }, 2000);  
        }
    });
    
    //submenu hover in\out
    $("#submenu").hover(function(){
        window.clearInterval(menuTimer);
    }, function(){ 
        menuTimer = window.setInterval(function () {
           var elm = $("#" + lastHovered).find("a");
           elm.attr("style", "background: white;");
           
           $("#submenu").slideUp();
           submenuOpened = false;
           window.clearInterval(menuTimer);    
        }, 2000);        
    });
});

/**
 * @author Alexander Farkas
 * v. 1.21
 */

(function($) {
	if(!document.defaultView || !document.defaultView.getComputedStyle){ // IE6-IE8
		var oldCurCSS = jQuery.curCSS;
		jQuery.curCSS = function(elem, name, force){
			if(name === 'background-position'){
				name = 'backgroundPosition';
			}
			if(name !== 'backgroundPosition' || !elem.currentStyle || elem.currentStyle[ name ]){
				return oldCurCSS.apply(this, arguments);
			}
			var style = elem.style;
			if ( !force && style && style[ name ] ){
				return style[ name ];
			}
			return oldCurCSS(elem, 'backgroundPositionX', force) +' '+ oldCurCSS(elem, 'backgroundPositionY', force);
		};
	}

	var oldAnim = $.fn.animate;
	$.fn.animate = function(prop){
		if('background-position' in prop){
			prop.backgroundPosition = prop['background-position'];
			delete prop['background-position'];
		}
		if('backgroundPosition' in prop){
			prop.backgroundPosition = '('+ prop.backgroundPosition;
		}
		return oldAnim.apply(this, arguments);
	};

	function toArray(strg){
		strg = strg.replace(/left|top/g,'0px');
		strg = strg.replace(/right|bottom/g,'100%');
		strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
		var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
		return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
	}

	$.fx.step. backgroundPosition = function(fx) {
		if (!fx.bgPosReady) {
			var start = $.curCSS(fx.elem,'backgroundPosition');

			if(!start){//FF2 no inline-style fallback
				start = '0px 0px';
			}

			start = toArray(start);

			fx.start = [start[0],start[2]];

			var end = toArray(fx.options.curAnim.backgroundPosition);
			fx.end = [end[0],end[2]];

			fx.unit = [end[1],end[3]];
			fx.bgPosReady = true;
		}
		//return;
		var nowPosX = [];
		nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
		nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];
		fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];
	};
})(jQuery);




