﻿/* -------------------------------------------------------------
 *	@Project: DirectLine
 *	@Author: Felix Toth (triplesense.de)
 *	@Created: 2009-06-22
 *	@Last edited: felix Toth, 2009-06-22
 * ------------------------------------------------------------- */
/* 
 * first of all, inject a css into the head of the document,
 * which styles the content like it's supposed to look with js enhancements
 * prevents flashing content before styling is applied by js plugins
 *
 */
var cssNode = document.createElement('link');
cssNode.type = 'text/css';
cssNode.rel = 'stylesheet';
cssNode.href = '/css/enhancements/index.css';
if (window.location.href.indexOf('s38795.gridserver') != -1) {
    cssNode.href = '/directline/css/enhancements/index.css';
}

cssNode.media = 'screen, print';
cssNode.title = 'enhanced';
document.getElementsByTagName("head")[0].appendChild(cssNode);

var service_url_hilfreich = "/app/helpful.do";
var jsonp_hilfreich = true;
var service_url_meistgeklickt = "http://webtrends.directline.de/dcsiwn8js100004j9nw8zw7in_1d1x/njs.gif?dcsuri=";

var service_url_autocomplete = "/includes/module/search-autocomplete.jsp";

/* 
 * then, dynamically load plugins
 *
 */
$(document).ready(function(){

    // enable caching	
    $.ajaxSetup({
        cache: true
    });
    
    // extend getScript to append javascript to head for easier debugging
    jQuery.extend({
        getScript: function(url, callback){
            var head = document.getElementsByTagName("head")[0];
            var script = document.createElement("script");
            script.src = url;
            
            // Handle Script loading
            {
                var done = false;
                
                // Attach handlers for all browsers
                script.onload = script.onreadystatechange = function(){
                    if (!done &&
                    (!this.readyState ||
                    this.readyState == "loaded" ||
                    this.readyState == "complete")) {
                        done = true;
                        if (callback) 
                            callback();
                        
                        // Handle memory leak in IE
                        script.onload = script.onreadystatechange = null;
                    }
                };
            }
            
            head.appendChild(script);
            
            // We handle everything using the script element injection
            return undefined;
        }
    });
    
    // load if needed
    
    
    
    if ($("select").length > 0) {
        if (!jQuery.skinSelect) {
            $.getScript("/js/jQuery.skinnedselect.js", function(){
                $("select").skinSelect();
            });
        }
        else {
            $('select').skinSelect();
        }
    };
    
    
    if ($("#dropdown").length > 0) {
        if (!jQuery.dropdown) {
            $.getScript("/js/jQuery.dropdown.js", function(){
                $("#dropdown").dropdown("#dropdown_content");
            });
        }
        else {
            $("#dropdown").dropdown("#dropdown_content");
        }
    };
    
    if ($(".faq-block").length > 0) {
        if (!jQuery.tabs) {
            $.getScript("/js/jquery.tabs.js", function(){
                var uid = 0;
                $(".faq-block").each(function(){
                    $(this).attr("id", "faq-block" + uid);
                    $("#faq-block" + uid).tabs("#faq-block" + uid + " div.answer", {
                        tabs: 'a.question',
                        effect: 'slide'
                    });
                    uid++;
                });
            });
        }
        else {
            var uid = 0;
            $(".faq-block").each(function(){
                $(this).attr("id", "faq-block" + uid);
                $("#faq-block" + uid).tabs("#faq-block" + uid + " div.answer", {
                    tabs: 'a.question',
                    effect: 'slide'
                });
                uid++;
            });
        }
    };
    
    
    if ($(".tabs").length > 0) {
        if (!jQuery.tabs) {
            $.getScript("/js/jquery.tabs.js", function(){
                $(".tabs").tabs(".tab-contents > div");
            });
        }
        else {
            $(".tabs").tabs(".tab-contents > div");
        }
    };
    
    
    if ($.browser.msie && $.browser.version < 7) {
        if (!jQuery.ifixpng) {
            $.getScript("/js/jquery.ifixpng2.js", function(){
                if ($("ul.white").length > 0) {
                    jQuery('#main_navigation ul li a').each(function(i){
                        $(this).css({
                            backgroundRepeat: "no-repeat",
                            backgroundImage: "url(/images/global/ui/main_navigation.png?" + i + ")"
                        }).ifixpng();
                    })
                    
                }
                jQuery('#header, #main_navigation').css({
                    backgroundRepeat: "no-repeat"
                }).ifixpng();
                
            });
        }
        else {
            if ($("ul.white").length > 0) {
                jQuery('#main_navigation ul li a').each(function(i){
                    $(this).css({
                        backgroundRepeat: "no-repeat",
                        backgroundImage: "url(/images/global/ui/main_navigation.png?" + i + ")"
                    }).ifixpng();
                })
            }
            jQuery('#header, #main_navigation').css({
                backgroundRepeat: "no-repeat"
            }).ifixpng();
        }
    }
    
    
    
    /* if ($("#campaign_visuals").length > 0) {
     $("#campaign_visuals .link").each(function(){
     $(this).hover(function(){
     $(this).find("img").css({
     marginTop: "-" + $(this).css("height")
     })
     }, function(){
     $(this).find("img").css({
     marginTop: "0"
     })
     })
     })
     }*/
    if ($(".feedback").length > 0) {
        if (!jQuery.hilfreich) {
            $.getScript("/js/jquery.hilfreich.js", function(){
                $(".feedback").hilfreich(service_url_hilfreich);
            });
        }
        else {
            $('.feedback').hilfreich(service_url_hilfreich);
        }
    };
    
    
    
    if ($(".meistgeklickt").length > 0) {
        if (!jQuery.hilfreich) {
            $.getScript("/js/jquery.meistgeklickt.js", function(){
                $(".meistgeklickt").meistgeklickt(service_url_meistgeklickt);
            });
        }
        else {
            $(".meistgeklickt").meistgeklickt(service_url_meistgeklickt);
        }
    };
    
    
    if ($("#werkstattsuche").length > 0) {
        if (!jQuery.werkstattsuche) {
            $.getScript("/js/jquery.werkstattsuche.js", function(){
                $("#werkstattsuche").werkstattsuche();
            });
        }
        else {
            $("#werkstattsuche").werkstattsuche();
        }
    };
    
    if (!jQuery.autocomplete) {
        $.getScript("/js/jquery.autocomplete.js", function(){
            $(".search-field").autocomplete({
                serviceUrl: service_url_autocomplete,
                minChars: 2
            });
        });
    }
    else {
        $(".search-field").autocomplete({
            serviceUrl: service_url_autocomplete,
            minChars: 2
        });
    }
    
    
    if (!jQuery.swapvalue) {
        $.getScript("/js/jquery.swapvalue.js", function(){
            $(".search-field").swapvalue();
            $("#werkstattsuche_plz").swapvalue();
        });
    }
    else {
        $(".search-field").swapvalue();
        $("#werkstattsuche_plz").swapvalue();
        
    }
    
    
    if (!jQuery.lightbox) {
        $.getScript("/js/jquery.lightbox.js", function(){
            $(".opens-popup").lightbox();
        });
    }
    else {
        $(".opens-popup").lightbox();
    }
    
    
    if (!jQuery.lightboxcontent) {
        $.getScript("/js/jquery.lightboxcontent.js", function(){
            $(".lightboxcontent-link").lightboxcontent();
        });
    }
    else {
        $(".lightboxcontent-link").lightboxcontent();
    }
    
	/* CO2-Rabatt Content-Slider / car models*/
	
    function initContentSlider(){
        $('.content-slider .forward').click(function(event){
            event.stopPropagation();
            var activeitem = $(this).parent('div').find('a.active');
            var n = activeitem.nextAll('a').length;
            if (n>2) // Überlaufschutz
            {
	            activeitem.removeClass('active');
	            if (activeitem.next('a').attr('rel') == 'model') {
	                activeitem.next().addClass('active');
	            }
	            else {
	                activeitem.parent('span').find('a:last').addClass('active');
	            }
	            $('#content-slider').scrollTo('a.active', {
	                axis: 'x',
	                duration: 250,
	                queue: true,
	                easing: 'swing'
	            });
            }
        })
        $('.content-slider .backward').click(function(event){
            event.stopPropagation();
            var activeitem = $(this).parent('div').find('a.active');
            activeitem.removeClass('active');
            if (activeitem.prev().attr('rel') == 'model') {
                activeitem.prev().addClass('active');
            }
            else {
                activeitem.parent('span').find('a:first').addClass('active')
            }
            $('#content-slider').scrollTo('a.active', {
                axis: 'x',
                duration: 250,
                queue: true,
                easing: 'swing'
            });
        })
        
    }
    
    if (!jQuery.scrollTo) {
        $.getScript("/js/jquery.scrollTo.js", function(){
            initContentSlider();
        });
    }
    else {
        initContentSlider();
    }
    
    $('#print').click(function(e){
        self.print();
    });
    
    $('.close').click(function(e){
        $('.home-message').hide();
    });
    
    if ($("#service_infotext").length > 0) {
		$('#service_infotext').hide();
		$("#service_barometer h6").mouseover(function () {
			$("#service_infotext").show();
		});
		$("#service_barometer").mouseout(function () {
			$("#service_infotext").hide();
		});
    };

	
	/* iPhone App */
	jQuery.fn.iphoneapp();	
	
});

function jc(m1, m2){
    return m1 + parseInt(Math.random() * (m2 - m1 + 1));
}



/* -------------------------------------------------------------
 *	iPhone App
 * ------------------------------------------------------------- */
jQuery.fn.iphoneapp = function() {
	$('.iphone-app-browser li a').click( function(e) {
		// alle zurücksetzen
		$('.iphone-app-browser li').removeClass('active');
		
		// aktuellen aktivieren
		var parent = $(e.target).parent();
		parent.toggleClass('active');
		return false;
    } );
};

