/* from http://www.quirksmode.org/js/cookies.html */
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}


var oHeights = new Object();
var oData    = new Object();
var iSize    = 12;

$(document).ready(function() {
    /*
    if (readCookie('popupOpened') != 1) {
        //window.open('/popup_prowein.html', 'Poup', 'width=741,height=275,status=no,menubar=no,scrollbars=no,resizable=no,top=50,left=50');
        $(document.createElement('div')).css('display', 'none').attr('id', 'popup_messe').html(
            '<div style="width: 741px; height 300px;"><a href="/fileadmin/media/images/brochures/RZ_Weinwirtschaft_020309.pdf" target="_blank"><img src="/fileadmin/media/images/system/popup_prowein.gif" alt="PDF herunterladen" border="0" /></a></div>'
        ).appendTo('body');
        //
        
        tb_show(null, '#TB_inline?height=275&width=741&inlineId=popup_messe', false);
        
        $('#popup_messe').remove();
    }
    
    createCookie('popupOpened', 1);
    */
    
    $("div#menu > ul > li > p").each(function() {
        if ($(this).children('a').attr("target") != "_blank") {
            oData[$(this).attr("id")] = $(this).parent().hasClass("active") ? "true" : readCookie($(this).attr("id"));
            oHeights[$(this).attr("id")] = $(this).parent().children("ul").height();
            
            if (oData[$(this).attr("id")] !== "true") {
                $(this).parent().children("ul").css( { "height": "0px", "display": "none", "padding-bottom": "0px", "opacity": 0.6 } );
                $(this).children("a").css("background-image", "url(/fileadmin/media/images/system/weinblatt_off.gif)");
            }
            else {
                $(this).children("a").css("background-image", "url(/fileadmin/media/images/system/weinblatt_on.gif)");
            }
            
            $(this).children('a').click(function() {
                eraseCookie($(this).parent().attr("id"));
                
                if (oData[$(this).parent().attr("id")] === "true") {
                    oData[$(this).parent().attr("id")] = "false";
                    createCookie($(this).parent().attr("id"), "false");
                    $(this).css("background-image", "url(/fileadmin/media/images/system/weinblatt_off.gif)");
                    
                    $(this).parent().parent().children('ul').stop()
                        .animate({ "padding-bottom": "0px", opacity: 0.6, height: "0px" }, { duration: 500 })
                        .queue(function() { $(this).css("display", "none").dequeue(); });
                }
                else {
                    oData[$(this).parent().attr("id")] = "true";
                    createCookie($(this).parent().attr("id"), "true");
                    $(this).css("background-image", "url(/fileadmin/media/images/system/weinblatt_on.gif)");
                    
                    $(this).parent().parent().children('ul').stop().css("display", "block")
                        .animate({ "padding-bottom": "8px", opacity: 1.0, "height": oHeights[$(this).parent().attr("id")] }, { duration: 500 });
                }
                
                return false;
            });
        }
    });
    
    if (window.location.pathname.substr(0, 5) == "/read") {
        $("div#header").append('<p><a href="#" id="dec">-</a>&nbsp;<img src="/fileadmin/media/images/system/aaa.gif">&nbsp;<a href="#" id="inc">+</a></p>');
        
        $("div#header a#dec").click(function() {
            if (iSize > 8) $("div#content").css("font-size", --iSize);
            return false;
        });
        
        $("div#header a#inc").click(function() {
            if (iSize < 20) $("div#content").css("font-size", ++iSize);
            return false;
        });
    }
    
    $("div#imageflow p img[src=/fileadmin/media/images/system/weinblatt_galerie.gif]").mousedown(function() {
        imageFlow.iVal = -1;
        imageFlow.startInterval();
    }).mouseup(function() {
        imageFlow.finishInterval();
    });
    
    $("div#imageflow p img[src=/fileadmin/media/images/system/weinblatt_galerie_links.gif]").mousedown(function() {
        imageFlow.iVal = 1;
        imageFlow.startInterval();
    }).mouseup(function() {
        imageFlow.finishInterval();
    });
});


window.imageFlow = {
    oDiv      : null,
    oInterval : null,
    iVal      : -1,
    iDelay    : 1,
    iWidth    : 0,
    iMargin   : 0,
    bStopIt   : false,
    
    startInterval : function(iTempDelay) {
        this.stopInterval();
        this.bStopIt = false;
        
        this.iWidth = parseInt($("div#imageflow div").css("width").substr(0, ($("div#imageflow div").css("width").length - 2)));
        this.oInterval = window.setInterval("imageFlow.moveImageflow()", 25);
    },
    
    finishInterval : function() {
        //this.stopInterval();
        this.bStopIt = true;
    },
    
    stopInterval : function() {
        if (this.oInterval) {
            window.clearInterval(this.oInterval);
            this.oInterval = null;
        }
    },
    
    moveImageflow : function() {
        this.iMargin = this.iMargin + this.iVal;
        $("div#imageflow div").css("margin-left", this.iMargin + "px");
        
        if ((this.iVal < 0)) {
            if (this.bStopIt == true) this.iVal++;
            else if (this.iVal > -10) this.iVal--;
        }
        else if ((this.iVal > 0)) {
            if (this.bStopIt == true) this.iVal--;
            else if (this.iVal < 10) this.iVal++;
        }
            
        if ((this.iVal == -1) || (this.iVal == 1)) {
            this.stopInterval();
        }
        else if (this.iMargin > 0) {
            this.iMargin = 0;
            $("div#imageflow div").css("margin-left", "0px");
            this.stopInterval();
        }
        else if (this.iMargin < -1 * (this.iWidth - 301)) {
            this.iMargin = (-1 * (this.iWidth - 301));
            $("div#imageflow div").css("margin-left", this.iMargin + "px");
            this.stopInterval();
        }
    }
}

