var topImg = 0;
var code_continue = true;
var code_timeout = 0;

function ShowTopImg (num) {
    jQuery("div#topLinkInact"+num).trigger("click", [code_continue]);
}

jQuery.fn.delayTopImg = function(time,num){
    clickNum = (num < 3) ? parseInt(num)+1 : 0;
    func = 'ShowTopImg ('+clickNum+')';
	this.each(function(){
		code_timeout = setTimeout(func,time);
	});
	
	return this;
};

//jQuery.noConflict();

isActive = new Array();

function HideObj (name) {
    if (!isActive[name]) {
        obj = "div#"+name;
        jQuery(obj).slideUp("fast");
    }
}

jQuery.fn.delay = function(time,obj){
	this.each(function(){
		setTimeout("HideObj ('"+obj+"')",time);
	});
	
	return this;
};

jQuery(document).ready(function($) {
    
     $("div.navi_button").each(function(){
        var position = $(this).position(); 

        thisID = $(this).attr("id");
        num = thisID.replace(/navi_button_/, "");
        menuPopup = 'div#navigation_popup_'+num;
        
        $(menuPopup).css('left', parseInt(position.left)+11);
	}); 
    
	var menuActive = false;

    $("div.navi_button").mouseover(function(){
    	thisID = $(this).attr("id");
        num = thisID.replace(/navi_button_/, "");
        menuPopup = 'navigation_popup_'+num;
        isActive[menuPopup] = true;

    	$("div#"+menuPopup).slideDown("fast");
	});    

    $("div.navi_button").mouseout(function(){
    	thisID = $(this).attr("id");
        num = thisID.replace(/navi_button_/, "");
        menuPopup = 'navigation_popup_'+num;
        isActive[menuPopup] = false;
        
        $(this).delay(100, menuPopup);  
	});    

    $("div.navigation_popup").mouseover(function(){    
    	thisID = $(this).attr("id");
        isActive[thisID] = true;
	});    
    
    $("div.navigation_popup").mouseout(function(){  
    	thisID = $(this).attr("id");
        isActive[thisID] = false;
        
        $(this).delay(100, thisID);  
	});    
    
    $("textarea#message_body").click(function(){  
    	$(this).html(""); 
	});    
    
/* Klappboxen */
    box_active = 0;
    
    $("div.animBox").each(function(i){
        if (i > -1) {
            $(this).hide();
            $("span#animHead_"+(i+1)).css('background-image', 'url(/templates/touchlines/img/icon_plus.gif)');
        }
    });
    
    $("span.animHead").click(function(){
        head_minus = "span#animHead_"+box_active;
        box_minus = "div#animBox_"+box_active;
        $(box_minus).slideUp();
        $(head_minus).css('background-image', 'url(/templates/touchlines/img/icon_plus.gif)');
        
        thisID = $(this).attr("id");
        num = thisID.replace(/animHead_/, "");
        if (num !=  box_active) {
            box_active = num;
            
            box_plus = "div#animBox_"+box_active;
            $(box_plus).slideDown();
            $(this).css('background-image', 'url(/templates/touchlines/img/icon_minus.gif)');
        } else {
            box_active = 0;        
        }

    });
    
/* Topaktuell */    

    $("div.topImgDiv").each(function(i){
        if (i>0) {
            $(this).hide();
            //$(this).css('left', '594px');
            $(this).find("img").css({'opacity':0.0});
            
            $("div#topLinkInact"+i).css({'opacity':0.5, 'display':'block'})
        } else {
            $("div#topLinkInact"+i).css({'opacity':0.0, 'display':'block'})
        }

    });
    
    $(this).delayTopImg(4000, 0);

    $("div.topLinkInact").click(function(event, cont){
    	thisID = $(this).attr("id");
        num = thisID.replace(/topLinkInact/, "");
        if (num != topImg) {
            $("div#topLinkInact"+topImg).fadeTo('slow', 0.4);
            $("div#topLinkInact"+num).fadeTo('slow', 0.0);
            $("div#topImgDiv"+topImg).css('z-index', 9);
            $("div#topImgDiv"+num).css('z-index', 10);
 /*       
            $("#topImgDiv"+num).show();
            $("#topImgDiv"+num).animate({'left':0}, 'slow', function(){
                $("div#topImgDiv"+topImg).hide();
                $("div#topImgDiv"+topImg).css({'left':594, 'z-index':10});
                topImg = num;
            });
*/            
            $("#topImgDiv"+topImg).find("img").animate({'opacity':0.0}, 500);
            $("#topImgDiv"+num).show();
            $("#topImgDiv"+num).find("img").animate({'opacity':1.0}, 500, function(){
                $("div#topImgDiv"+topImg).hide();
                topImg = num;
            });
        }
        
        if (cont)  {
            $(this).delayTopImg(8000, num);
        } else {
            code_continue = false;
            clearTimeout (code_timeout);
        }

        return false;
    });
    
    $("div.topLinkInact").live('mouseover', function(){    
        $(this).css({'opacity':0.0});
    });
        
    $("div.topLinkInact").live('mouseout', function(){    
        thisID = $(this).attr("id");
        num = thisID.replace(/topLinkInact/, "");
        if (num != topImg) {
            $(this).css({'opacity':0.4});
        }
    });
    
/* Image Popup */    
    

    $("div#bigImgLink, img#products_image").click(function(){
        img = $("div#prodImg_container").html();
        if (img) {
            $("div#popup_product_img").html(img);
            $("div#popup_product_img").parent().parent().fadeIn('fast');
        }

        /*
        $("div#popup_product_img").gzoom({sW: 548,
										sH: 700,
										lW: 2500,
										lH: 3200,
										lighbox : false
										});   
                                        */
    });
    
    $("div.detail_close").click (function(){
        $("div#popup_product_img").parent().parent().fadeOut('fast');
    });    
    
    

/* Themenfilter */
        
    $("input.theme_select").click(function(){ 
        filter = '';
        $("input.theme_select").each(function(){
            if ($(this).attr('checked')) filter += ($(this).val() + ',');
        });
        document.themesfilter.submit();
    });    
	
/* Themenseite Kategoriefilter */
    $("input.theme_checked_styles").click(function(){ 
        filter = '';
		$("img#axloader").show();
        $("input.theme_checked_styles").each(function(){
            if ($(this).attr('checked')) filter += ($(this).val() + ',');
			$(this).hide();
        });
        document.stylesfilter.submit();
    });    
    
    
    $("input#to_cart").click(function(){ 
        $("input#submit_target").val('');
        //$("input#add_product").val('0');
    });
    
})
