/* REQUIRES context-teasers.js, scrollbars.js */

/**
 * Rollos
 */

var rolloId = 1;
var oldRolloId = 1;
var anchor = "";
var showSpecial = "";

var rollo = new Array();
rollo[0] = new Object();
rollo[0]["size"] = new Object();
rollo[1] = new Object();
rollo[1]["size"] = new Object();
rollo[2] = new Object();
rollo[2]["size"] = new Object();
rollo[3] = new Object();
rollo[3]["size"] = new Object();
rollo[4] = new Object();
rollo[4]["size"] = new Object();

/*
$(document).ready(function{
	if ($.browser.mozilla) {
		//$(".rollo1 *").css("overflow",'hidden')
		//$(".rollo2 *").css("overflow",'hidden')
		//$(".rollo3 *").css("overflow",'hidden')
	}
})*/

var Rollos = {
  
	initLink: function(rolloId) {
		Rollos.logger("Rollos.initLink("+rolloId+")");
		if (rollo[rolloId]["url"].length) {
			$(".rollo"+rolloId+" .top-bar h1 a").unbind("click");
			$(".rollo"+rolloId+" .top-bar h1 a").click(function() {
                window.document.title = rollo[rolloId]["title"];
				Rollos.resize(rolloId,rollo[rolloId]["size"]["act"],rollo[rolloId]["size"]["inact"]);
				return false;
			});
		}
	},
	
	getSizeOld: function(url,rolloId) {
		Rollos.logger("Rollos.getSize("+url+","+rolloId+")");
		params = "";
		urlParam = addPostSlash(url);
		url = '/typo3conf/ext/bm_ent_page_info/pi1/bmentpageinfo.php';
		if (url.indexOf("?") == -1) { params += "?"; }
		if (url.indexOf("type=2") == -1) { params += "&type=2"; }
		if (url.indexOf("url=") == -1) { params += "&url="+urlParam; }
		if (url.indexOf("ext=tx_bmentpageinfo_pi1") == -1) { params += "&ext=tx_bmentpageinfo_pi1"; }
		$.ajax({
			url: url+params,
			dataType: "json",
			success: function(data) {
                window.document.title = data.title+" - ENTEGA - Energie der nächsten Generation";
				rollo[rolloId]["title"] = window.document.title;
				if ((data.rolloSizeAct > 0) && (data.rolloSizeInact > 0) && (rolloId)) {
					Rollos.resize(rolloId,data.rolloSizeAct,data.rolloSizeInact);
				}
			}
		});
	},
	
	getSize: function(rolloUrl,rolloId) {
		Rollos.logger("Rollos.getSize("+rolloUrl+","+rolloId+")");
		params = "";
		rolloUrl = addPostSlash(rolloUrl);
		url = '/typo3conf/ext/bm_ent_page_info/pi1/bmentpageinfo.php';
		if (url.indexOf("?") == -1) { params += "?"; }
		if (url.indexOf("type=2") == -1) { params += "&type=2"; }
		if (url.indexOf("uid=") == -1) { params += "&uid="+pid; }
		if (url.indexOf("rollo=") == -1) { params += "&rollo="+rolloId; }
		if (url.indexOf("ext=tx_bmentpageinfo_pi1") == -1) { params += "&ext=tx_bmentpageinfo_pi1"; }

		$.ajax({
			url: url+params,
			dataType: "json",
			success: function(data) {
                window.document.title = data.title+" - ENTEGA - Energie der nächsten Generation";
				rollo[rolloId]["title"] = window.document.title;
				if ((data.rolloSizeAct > 0) && (data.rolloSizeInact > 0) && (rolloId)) {
					Rollos.resize(rolloId,data.rolloSizeAct,data.rolloSizeInact);
				}
			}
		});

	},
	
	resize: function(rolloId, rolloSizeAct, rolloSizeInact) {
		
    Rollos.logger("Rollos.resize("+rolloId+","+rolloSizeAct+","+rolloSizeInact+")");
		var scrollheight = 0;
		
		if (rolloId == 4) {
			rolloSizeAct = 504;
			rolloSizeInact = 20;
		}
		
		rollo[rolloId]["size"]["act"] = rolloSizeAct;
		rollo[rolloId]["size"]["inact"] = rolloSizeInact;
		
		//--------------Andreas Pfau-----------------------------
		var activePane = null
		var inactivePanes = new Array()
		
		for (var i=1;i<=4;i++) {
			if (i == rolloId) activePane = i
			else inactivePanes.push(i)
		}
		
		//It's important to roll first the rollos that gone be smaller.
		//and after that to unroll the rollos that going to be bigger.	
		var height = $(".rollo"+activePane).css("height")
		height = height.substring(0,height.indexOf('px'))
		
		
		//the active rollo is still active but smaller.
		if (parseInt(height) >= parseInt(rolloSizeAct)) {

            //activate inactive pane
			this.activatePane(activePane,rolloSizeAct)

			//deactivate active panes 
			this.deactivatePanes(inactivePanes,rolloSizeInact)
		}
		//and the normal case, the active rollo is going to be bigger.
		else {	
		
            //deactivate active panes 
			this.deactivatePanes(inactivePanes,rolloSizeInact)
			
			//activate inactive pane
			this.activatePane(activePane,rolloSizeAct)
		}
		
		$(".rollo4").css("height","0px");
		
		ContextTeasers.init();		
	},
	
	/**
	 * author: Andreas Pfau
	 * This method deactivates one given pane.
	 * params: integer inactivaPaneId, integer: size
	 * date: 15.09.2009	 
	 **/
	deactivatePane: function(inactivePane, rolloSizeInact){
		Rollos.logger("Rollos.deactivatePane("+inactivePane+","+rolloSizeInact+")");
		
    var height = $(".rollo"+inactivePane).css("height")
		
		if (height) {
      height = height.substring(0,height.indexOf('px'))
		}else{
      height=0
    }
		
		if (parseInt(height) != parseInt(rolloSizeInact)) {
			if (inactivePane == 4) {
				rolloSizeInact = 0;
				$(".rollo4").css("margin-top","0");
			}
			
			scrollheight = (rolloSizeInact - 30);
			if (scrollheight < 0) {
				scrollheight = 0;
			}
			
            Flash.resetFlashLayer(inactivePane);

			// Im Rollo2 gibt es mit Flash eine andere Höhe
            if (inactivePane == 2) {
                $(".rollo"+inactivePane+" .content").css("height",scrollheight+"px");
                $(".rollo"+inactivePane+" .content object").css("height",scrollheight+30+"px");
			} else {
                $(".rollo"+inactivePane+" .content").css("height",scrollheight+"px");
                $(".rollo"+inactivePane+" .content .holder").css("display","none");
                $(".rollo"+inactivePane+" .home").css("height",rolloSizeInact+"px");
                if (rollo[inactivePane]["url"]) {
                    $(".rollo"+inactivePane).click(function() {
//                    $(".rollo"+inactivePane).live("click",function() {
                        anchor = "";
                        url = removeDomain(rollo[inactivePane]["url"]);
                        Google.track(url);
                        $.historyLoad(url);
                        return false;
                    });
                    $(".rollo"+inactivePane).css("cursor","pointer");
                } else {
                    if (inactivePane != 4) {
                        var ausdruckHome1 = /\/\w{2}\/home/;
                		var ausdruckHome2 = /\/home/;
                		if ((!ausdruckHome1.exec(document.location.pathname)) && (!ausdruckHome2.exec(document.location.pathname))) {
                            rollo[inactivePane]["url"] = document.location.pathname;
                        } else {
                            rollo[inactivePane]["url"] = addPreSlash($(".rollo"+inactivePane+" .rollo-headline a").attr("href"));
                        }

                        $(".rollo"+inactivePane).click(function() {
//                        $(".rollo"+inactivePane).live("click",function() {
                            anchor = "";
                            url = removeDomain(rollo[inactivePane]["url"]);
                            Google.track(url);
                            $.historyLoad(url);
                            return false;
                        });
                        $(".rollo"+inactivePane).css("cursor","pointer");
                    }
                }
            }

			$(".rollo"+inactivePane).animate({
				height: rolloSizeInact+"px"
			},{queue:true});

			if (($.browser.msie) && ($.browser.version < 8)) {
				$(".rollo"+inactivePane+" .container-center").css("overflow","hidden");
			}

			ContextTeasers.markRolloAsInactive(inactivePane);
			
			this.hideSubMenu(inactivePane);
		}	
	},

	/**
	 * author: Andreas Pfau
	 * This method deactivates all given panes.
	 * params: array(integer) inactivaPaneIds, integer: size
	 * date: 15.09.2009	 
	 **/	
	deactivatePanes: function(panes,rolloSizeInact){
        
        Rollos.logger("Rollos.deactivatePanes("+panes+","+rolloSizeInact+")");

        if ($.browser.mozilla) {
    		//$(".rollo1 .content div[id!=cart_container]").css("overflow",'hidden')
    		$(".rollo2 .content div").css("overflow",'hidden')
    		$(".rollo3 .content div[class=holder]").css("overflow",'hidden')
    	}

		for (var i=0; i<panes.length; i++) {
	
      this.deactivatePane(panes[i],rolloSizeInact)
		}
		
	},

	/**
	 * author: Andreas Pfau
	 * This method activates one given pane.
	 * params: integer activaPaneId, integer: size
	 * date: 15.09.2009
	 **/	
	activatePane: function(activePane,rolloSizeAct) {
        
        Rollos.logger("Rollos.activatePane("+activePane+","+rolloSizeAct+")");
     
     		   
        //activate inactive pane
		if (activePane== 4) {
			rolloSizeInact = 0;
			$(".rollo4").css("margin-top","2px");
			$(".rollo4").css("height",($(".rollo4").height()-2)+"px");
		}
		
		$(".rollo"+activePane).animate({
			height: rolloSizeAct+"px"
		},{queue:true});
		
		scrollheight = (rolloSizeAct - 84);
		
		ContextTeasers.markRolloAsActive(activePane, rolloSizeAct);
		
		this.showSubMenu(activePane);

		// Im Rollo2 gibt es mit Flash eine andere Höhe
		if (activePane == 2) {
//			$(".rollo"+activePane+" .top-bar").css("background","none");
//			$(".rollo"+activePane+" .content").css("left","0");
            $(".rollo"+activePane+" .content").css("height",scrollheight+90+"px");
			$(".rollo"+activePane+" .content object").css("height",scrollheight+90+"px");
			
		} else {

			$(".rollo"+activePane).unbind("click");
//			$(".rollo"+activePane).die("click");
			$(".rollo"+activePane).css("cursor","default");

			Scrollbars.remove($(".rollo"+activePane+" .container-center"));
			$(".rollo"+activePane+" .container-center").css("height",scrollheight+"px");
			$(".rollo"+activePane+" .container-center").css("margin","0");
			$(".rollo"+activePane+" .container-center").css("overflow","auto");
			
			
//			$(".rollo"+activePane+" .content").css("height",scrollheight+"px");
			$(".rollo"+activePane+" .content").css("height","auto");
			$(".rollo"+activePane+" .content .holder").css("display","block");
//			$(".rollo"+activePane+" .content").css("overflow","auto");
			
			Scrollbars.init(activePane);
		}

        Flash.restartFlashLayer(activePane);

		if ($(".rollo"+activePane+" .swf,.rollo"+activePane+" .flv").size()) {
			Flash.init(swf);
			swf = new Array();
		}


        // Sprungmarke bzw. Top Link
        Rollos.initAnchor();

		//console.info('activate rollo '+activePane)		
	},
	
	getRolloByHash: function(hash) {
		Rollos.logger("Rollos.getRolloByHash("+hash+")");
		var rollo1 = /\/(de\/|)produkte-und-service/;
		var rollo2 = /\/(de\/|)entega-interaktiv/;
		var rollo3 = /\/(de\/|)unternehmen/;
		
		//20100113 tpo bm : SEO Seiten sollen im Rollo 1 angezeigt werden.
		var rollo4 = /\/(de\/|)(anbieter-versorger|preis-vergleich|energie-rund-ums-haus|alternative-energie|energie|nachhaltigkeit)/;
		
		var rolloId = 4;
		if (rollo1.exec(hash)) {
			rolloId = 1;
		} else if (rollo2.exec(hash)) {
			rolloId = 2;
		} else if (rollo3.exec(hash)) {
			rolloId = 3;
		} else if (rollo4.exec(hash)) {
			rolloId = 1;
		}

		return rolloId;
	},
	
	showSubMenu: function(rolloId) {
		Rollos.logger("Rollos.showSubMenu("+rolloId+")");
		if ($(".rollo"+rolloId+" .tablist2:visible").size()) {
			$(".rollo"+rolloId+" .tablist2").show();
		} else {
			$(".rollo"+rolloId+" .tablist2").css("height","20px");
			$(".rollo"+rolloId+" .tablist2").slideDown(500);
		}
		$(".rollo"+rolloId+" .tablist3").show();
		$(".rollo"+rolloId+" .jScrollPaneTrack, .rollo"+rolloId+" .jScrollArrowUp, .rollo"+rolloId+" .jScrollArrowDown").show();
        $(".rollo"+rolloId+" .container-center").show();
	},
	
	hideSubMenu: function(rolloId) {
		Rollos.logger("Rollos.hideSubMenu("+rolloId+")");
		$(".rollo"+rolloId+" .tablist2,.rollo"+rolloId+" .tablist3").hide();
		$(".rollo"+rolloId+" .jScrollPaneTrack, .rollo"+rolloId+" .jScrollArrowUp, .rollo"+rolloId+" .jScrollArrowDown").hide();
		if (rolloId != 2) {
            $(".rollo"+rolloId+" .container-center").hide();
        }
        ContextTeasers.removeAllTeasers();
	},
	
	getContent: function(url,targetType,startpage) {
        Rollos.logger("Rollos.getContent("+url+","+targetType+","+startpage+")");
        rolloUrl = url;
		if (startpage) {
			
			var containerToUpdate = '#content-full-width';
	
			if ($(".rollo1").css("height") != "210px") {
//				$("#content-full-width").append('<div class="overlay"></div><div class="overlayContentContainer"><div class="overlayContent">Please wait ...<br /><img src="/fileadmin/templates/images/ajax-loader.gif" alt="" /></div></div>');

				$.ajax({
//					url: url+"?type=3",
					url: url,
					dataType: "html",
					error: function(XMLHttpRequest, textStatus, errorThrown) {				
						Rollos.showError(containerToUpdate, XMLHttpRequest, textStatus, errorThrown);
					},
					success: function(html) {
            
//                        Google.track(url);
        				
//						Rollos.removeOverlay(containerToUpdate);
	
						for (i=1; i<=3; i++) {
							$(".rollo"+i).html($(html).find(".rollo"+i).html());
							if (!$(".rollo"+i).parent().attr("class")) {
								$(".rollo"+i).wrap('<div class="fragment1"></div>');
							}
							rollo[i]["url"] = "";
							History.initRollo(i);
//							initEasyTooltip(i);
//							ImageSliders.init(i);
							Rollos.initLink(i);
							Thickbox.initInRollo(i);
							ENTEGA.initHomepage(i);
							//$(".rollo"+i+" .content").css("overflow","auto");
							$(".rollo"+i+" .container-center").css("overflow","auto");
						}					
						Rollos.resize(3, 150, 210);
						oldRolloId = targetType;
					}
				});
			}
			
		} else {
	
			type = 1;
			
			if ($("#TB_ajaxContent").size()) {
				var containerToUpdate = "#TB_ajaxContent";
				targetType = 0;
				type = 3;
			} else {
				var containerToUpdate = "#content-full-width .rollo"+targetType;
			}
            
//			$(containerToUpdate).append('<div class="overlay"></div><div class="overlayContentContainer"><div class="overlayContent">Please wait ...<br /><img src="/fileadmin/templates/images/ajax-loader.gif" alt="" /></div></div>');
			
			newPageViaAjax = 1;
            if (oldRolloId != targetType) {
                if ((rollo[targetType]["url"] == rolloUrl) && (targetType != 2)) {
                    newPageViaAjax = 0;
                }
            }
			
			params = "";
			if (url.indexOf("?") == -1) { params += "?"; }
			if (url.indexOf("type="+type) == -1) { params += "&type="+type; }

            if (newPageViaAjax) {
    			$.ajax({
    				url: url+params,
    				dataType: "html",
    				error: function(XMLHttpRequest, textStatus, errorThrown) {		
    					Rollos.showError(containerToUpdate, XMLHttpRequest, textStatus, errorThrown);
    				},
    				success: function(html) {

//                        Google.track(url); 

    //					Rollos.removeOverlay(containerToUpdate);
    //					$(containerToUpdate).animate({
    //						opacity:0
    //					}, 300, "linear", function() {
    
    							//$(containerToUpdate).html('<div class="rollo-home">'+html+'</div>');
    							$(containerToUpdate).html(html);
    							
    							if (oldRolloId != rolloId) {
    								$(".rollo"+rolloId+" .tablist2").hide();
    							}
    
    
    							if (targetType) {
    								History.initRollo(targetType);
//   								Rollos.initLink(targetType);
                                    ENTEGA.initRollo(targetType)
//    								ENTEGA.initRollo(1);
//    								ENTEGA.initRollo(2);
//    								ENTEGA.initRollo(3);
//    								ENTEGA.initRollo(4);
    
                					Rollos.getSize(rolloUrl,targetType);
                					rollo[targetType]["url"] = url;
                					oldRolloId = targetType;
    
    							} else {
    								TableSorter.initThickbox();
    								Scrollbars.initThickbox();
    								Form.initThickbox();
    								Thickbox.setThickboxTitle();
    							}
    							
    //							$(containerToUpdate).animate({
    //								opacity:1
    //							}, 300, "linear");
    //						}
    //					);
    					
    				}
    			});
    		} else {
                Rollos.resize(targetType,rollo[targetType]["size"]["act"],rollo[targetType]["size"]["inact"]);
                rollo[targetType]["url"] = url;
                oldRolloId = targetType;
            }
		}

	},
	
	showError: function(containerToUpdate, XMLHttpRequest, textStatus, errorThrown) {
		Rollos.logger("Rollos.showError("+containerToUpdate+","+XMLHttpRequest+","+textStatus+","+errorThrown+")");
		Rollos.removeOverlay(containerToUpdate);
		$(containerToUpdate).animate({
			opacity:0
		}, 300, "linear", function() {
				$(containerToUpdate).html($("#error").html());
				$(containerToUpdate).wrapInner('<div class="ajaxError"></div>');
				$(containerToUpdate).animate({
					opacity:1
				}, 300, "linear");
			}
		);
	},
	
	removeOverlay: function(containerToUpdate) {
		Rollos.logger("Rollos.removeOverlay("+containerToUpdate+")");
		$(containerToUpdate + " .overlay").remove();
		$(containerToUpdate + " .overlayContentContainer").remove();
	},

	removeLastMargin: function(rolloId) {
        Rollos.logger("Rollos.removeLastMargin("+rolloId+")");
        $(".rollo"+rolloId+" .middle > div[class*=content-element]:last").css("margin-bottom","0");
        $("#content-lightbox .middle > div[class*=content-element]:last").css("margin-bottom","0");
    },

	initAnchor: function() {
        Rollos.logger("Rollos.initAnchor()");
        if (anchor) {
            if ($(".rollo"+rolloId+" #"+anchor).size()) {
                Rollos.jumpToAnchor(rolloId,anchor);
            }
        }
    },

	// Sprung an Sprungmarkenziel
    jumpToAnchor: function(rolloId,scrollDestination) {
        Rollos.logger("Rollos.jumpToAnchor("+rolloId+","+scrollDestination+")");
        if ($(".rollo"+rolloId+" .jScrollPaneContainer").size()) {
            $paneTarget = $(".rollo"+rolloId+" .container-center");
            $paneTarget[0].scrollTo("#"+scrollDestination);
        } else {
            $paneTarget = $(".rollo"+rolloId+" .container-center");
            $paneTarget.scrollTo("#"+scrollDestination);
        }
    },

    logger: function(logstring) {
		/* log(logstring); */
	}
	
};
