/**
 * History - Ajax Fallback
 */


var History = {
	init: function() {
		History.logger("History.init()");
		// The callback is called at once by present location.hash. 
		$.historyInit(pageload, "");
		History.initRollo(1);
		History.initRollo(2);
		History.initRollo(3);
		History.initRollo(4);
		History.initMainNav();
	},

	initRollo: function(rolloId) {
		History.logger("History.initRollo("+rolloId+")");
        
        Content.initUsertypeSwitch(rolloId);
        
        $(".rollo"+rolloId+" a").each(function() {
			// If the link is part of an anythingSlider control
			// don't unbind the click event or it will break the slider
			if ($(this).parents('.anythingControls').size() == 0 && $(this).parents('.aufklappmodul').size() == 0) {
				$(this).unbind("click");
			}

			// Links containing javascript code without ajax please
			if ($(this).attr('href') && $(this).attr('href').indexOf('javascript') > -1) {
				$(this).addClass('noajax');
			}
		});
		$(".rollo"+rolloId+" a[href*=mailto]").addClass("email");
		$(".rollo"+rolloId+" a[class*=clicktotalk-submit]").addClass("noajax");
		$(".rollo"+rolloId+" a[class*=download]").addClass("noajax");
		$(".rollo"+rolloId+" a[class*=download]").attr("target","_blank");
		
		// Modified by Andreas Pfau 09.09.2009
		//$(".rollo"+rolloId+" iframe a").addClass("noajax");
		// Modified by Andreas tpo 28.01.2010
		//$(".rollo"+rolloId+" a.captchaRefreshLink").unbind("click");
		//-------------------------
		
		$(".rollo"+rolloId+" a[target!=thickbox][class!=external-link][class!=mail][class!=to-top]").click(function(){
			// If the link is part of an anythingSlider control
			// do absolutely nothing
			if ($(this).parents('.anythingControls').size() == 0 && $(this).parents('.aufklappmodul').size() == 0) {
	            anchor = "";
    	        url = addPreSlash(addPostSlash(this.pathname));
				Google.track(url);
				Form.setCustomerSelectorAction(url);
	            if ($(this).hasClass("noajax")) {
   		            $(".rollo"+rolloId).unbind("click");
   		            if ($(this).attr('target') == '_blank') {
   		            	window.open($(this).attr("href"));
					} else {
	       		        document.location.href = $(this).attr("href");
					}
           		} else {
	               	$.historyLoad(url);
    	       	}
	  			return false;
    	    }
		});
        
		$(".rollo"+rolloId+" a.noajax").unbind("click");
		$(".rollo"+rolloId+" a.email").unbind("click");
		$(".rollo"+rolloId+" a[class*=noclickhandler]").unbind('click');
		$(".rollo"+rolloId+" iframe a").unbind("click");
		$(".rollo"+rolloId+" a.captchaRefreshLink").unbind("click");

        // Sprungmarke bzw. Top Link
		var actuellUrl = getActuellUrl();
        if ($(".rollo"+rolloId+" .content a.internal-link,.rollo"+rolloId+" .content a.to-top").size()) {
    		$(".rollo"+rolloId+" .content a.internal-link,.rollo"+rolloId+" .content a.to-top").each(function() {
        		if ($(this).attr("href") && !$(this).hasClass('detaillink')) {
					var link = $(this).attr("href");
	                if (link.indexOf("#") > -1) {
	
	                    var link = addPreSlash(addPostSlash(removeHash(link)));
	                    $(this).unbind("click");
	                    $(this).addClass("noajax");
	                    $(this).click(function() {
	
	                        if (link == actuellUrl) {
	                            // Sprungmarke auf der gleichen Seite
	                            scrollDestination = getHash($(this).attr("href"));
	                            Rollos.jumpToAnchor(rolloId,scrollDestination);
	                            anchor = "";
	                        } else {
	                            // Sprungmarke auf einer anderen Seite
	                            url = addPreSlash(addPostSlash(this.pathname));
	                            anchor = getHash($(this).attr("href"));
	                            //scrollDestination = getHash($(this).attr("href"));
	                            //if (url.indexOf("/anchor/") == -1) { url += "anchor/"+scrollDestination+"/"; }
	                			Google.track(url);
	                			Form.setCustomerSelectorAction(url);
	                            $.historyLoad(url);
	                			return false;
	                        }
	                        
	                        //
	                        return false;
	                    });
	                }
	            }
            });
        }
		
//		$(".rollo"+rolloId+" a.to-top").unbind("click");
//		$(".rollo"+rolloId+" #clicktotalk-submit").unbind("click");
	},

	initMainNav: function() {
		History.logger("History.initMainNav()");
		$("#main_nav a[target!=thickbox],#footer a[target!=thickbox],#footer2 a[target!=thickbox]").unbind("click");
		$("#main_nav a[target!=thickbox][target!=_blank][class!=external-link]").click(function(){
            anchor = "";
            url = addPreSlash(addPostSlash(this.pathname));
            Google.track(url);
            $.historyLoad(url);
			return false;
		});
		$("#footer a[target!=thickbox][target!=_blank][class!=external-link]").click(function(){
            anchor = "";
            url = addPreSlash(addPostSlash(this.pathname));
            Google.track(url);
			$.historyLoad(url);
			return false;
		});
		$("#footer2 a[target!=thickbox][target!=_blank][class!=external-link]").click(function(){
            anchor = "";
            url = addPreSlash(addPostSlash(this.pathname));
            Google.track(url);
			$.historyLoad(url);
			return false;
		});
//		$("#main_nav a[target=_blank]").unbind("click");
		$("#footer2 a[class=noajax]").unbind("click").click(function() {
			document.location.href = $(this).attr('href');
		});
	},

	initThickbox: function() {
		History.logger("History.initThickbox()");
		$("#TB_ajaxContent a").each(function() {
			if ($(this).parents('.aufklappmodul').size() == 0) {
				$(this).unbind("click");
			}
		});
		$("#TB_ajaxContent a[href*=mailto]").addClass("email");
		$("#TB_ajaxContent a[class*=clicktotalk-submit]").addClass("noajax");
		$("#TB_ajaxContent a[target!=thickbox][class!=external-link][class!=mail]").each(function() {
			if ($(this).parents('.aufklappmodul').size() == 0) {
				$(this).click(function() {
		            anchor = "";
		            url = addPreSlash(addPostSlash(this.pathname));
		            Google.track(url);
		            $.historyLoad(url);
		            return false;
				});
			}
		});
		$("#TB_ajaxContent a.email").unbind("click");
		$("#TB_ajaxContent a.noajax").unbind("click");
		$("#TB_ajaxContent .csc-sitemap a").unbind("click");
//		$("#TB_ajaxContent #clicktotalk-submit").unbind("click");
	},
	
	initContextTeaser: function() {
		History.logger("History.initContextTeaser()");
		$(".individualcontent_pi4 a").unbind("click");
		$(".individualcontent_pi4 a[target!=thickbox][class!=external-link]").click(function(){
            anchor = "";
            url = addPreSlash(addPostSlash(this.pathname));
            Google.track(url);
            $.historyLoad(url);
			return false;
		});
	},
	
	load: function(hash) {
		//IE6 warning text just on the start page
/**
		if ( $('#ie6-txt').length>0 ) {
      try {
        var pathname = window.location.pathname
        pathname = pathname.replace(/\//g,'').replace(/#/g,'')
        var hash = window.location.hash.replace(/#/,'')

        if ( pathname=='dehome' && hash.length==0) {
          $('#ie6-txt').show();
        } else {
          $('#ie6-txt').hide();
        }
       }catch(e){alert(e.description); $('#ie6-txt').hide();}     
    }
*/
    
        History.logger("History.load("+hash+")");
		var ausdruckHome1 = /\/\w{2}\/home/;
		var ausdruckHome2 = /\/home/;
	
		// Jede Ajax Seite ausser Home
		if ((hash) && (!ausdruckHome1.exec(hash)) && (!ausdruckHome2.exec(hash))) {	

			var url = window.location.protocol+"//"+window.location.hostname+hash;
			rolloId = Rollos.getRolloByHash(hash);
			Rollos.getContent(url,rolloId,false);
	
		} else {

			if (hash) {	// Startseite mit Hash
				var url = window.location.protocol+"//"+window.location.hostname+hash;
				Rollos.getContent(url,1,true);
			} else {
				url = window.location.href;
				if ((rollo[1]["url"]) || (rollo[2]["url"]) || (rollo[3]["url"]) || (rollo[4]["url"])) {
					if ((!ausdruckHome1.exec(url)) && (!ausdruckHome2.exec(url))) {
						url = url.replace( /#.*/, "");
						rolloId = Rollos.getRolloByHash(url);
						Rollos.getContent(url,rolloId,false);
					} else {
						Rollos.getContent("/",1,true);				
					}
				} else { // Direkt Link ohne Hash
					if ((!ausdruckHome1.exec(url)) && (!ausdruckHome2.exec(url))) {
						rolloId = Rollos.getRolloByHash(url);
						Rollos.getSize(url,rolloId);
					} else {
						
					}
				}
			}
	
		}
	},
	
	logger: function(logstring) {
		//log(logstring);
	}
};

