﻿/* -------------------------------------------------------------
 *	@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://statse.webtrendslive.com/dcs6wk6vjvz5bd098x0djtzgh_7u6h/dcs.gif?dcssip=www.directline.de&WT.z_faq=1&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 ($(".radiostars").length > 0) 
    {
    	var current =0;
    	var rated = 0;
        $(".radiostars input").each(function(i){
            $(this).css({
                'visibility': 'visible',
                'opacity': 0
            });
            $(this).hover(function()
            {
               	 $(this).parent().removeClass();
               	 cl = 'radiostars grade_' + i;
               	 if (rated==1)
               	 {
               	 	cl = 'radiostars rated grade_' + i;
               	 }
               	 $(this).parent().addClass(cl);               
            });
            $(this).click(function()
            {
            	current = i;
            	rated = 1;
            	$(this).parent().removeClass();
            	cl = 'radiostars rated grade_' + i;
               	$(this).parent().addClass(cl);
            });
            
        });
   
        $(".radiostars").mouseleave(function()
        {
        	if ($(this).hasClass('rated')) 
        	{
           		$(this).removeClass();
           		cl = 'radiostars grade_' + current;
              	if (rated==1)
                {
                	cl= 'radiostars rated grade_' + current;
                }
            	$(this).addClass(cl);
            }
            else 
            {
               	$(this).removeClass();
               	$(this).addClass('radiostars');
            }
        });
   
    };

    
    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();	
	
	/* FAQ Element */
	jQuery.fn.faq();
		
	
});

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;
    } );
};

/* -------------------------------------------------------------
 *	Auszeichnungen Lightbox
 * ------------------------------------------------------------- */
jQuery.fn.awardLightbox = function() {
	
	// PrevLink
	$('.prev-award').live('click', function(e) {
		window.number--;
		jQuery.fn.awardLightboxDetails(window.number);
		return false;
	});
	
	// NextLink
	$('.next-award').live('click', function(e) {
		window.number++;
		jQuery.fn.awardLightboxDetails(window.number);
		return false;
	});
};

/* -------------------------------------------------------------
 *	QR-Codes Lightbox
 * ------------------------------------------------------------- */
jQuery.fn.qrcodeLightbox = function() {
	
	// PrevLink
	$('.prev-qrcode').live('click', function(e) {
		window.number--;
		jQuery.fn.qrcodeLightboxDetails(window.number);
		return false;
	});
	
	// NextLink
	$('.next-qrcode').live('click', function(e) {
		window.number++;
		jQuery.fn.qrcodeLightboxDetails(window.number);
		return false;
	});
};


/* -------------------------------------------------------------
 *	Auszeichnungen Lightbox - Navigation
 * ------------------------------------------------------------- */

jQuery.fn.awardLightboxDetails = function() {	
	
	// Navigationselemente
	$('.prev-award').show();
	$('.next-award').show();
	
	if( window.number <= 1 ) {
		$('.prev-award').hide();
	} else if( window.number >= $('.awards .lightboxcontent-details li').size() ) {
		$('.next-award').hide();
	}

	// Awardsdetails einblenden
	$('.awards-details li').hide();
	$('.awards-details li:nth-child(' + window.number + ')').fadeIn();
}

/* -------------------------------------------------------------
 *	QR-Codes Lightbox - Navigation
 * ------------------------------------------------------------- */

jQuery.fn.qrcodeLightboxDetails = function() {	
	
	// Navigationselemente
	$('.prev-qrcode').show();
	$('.next-qrcode').show();
	
	if( window.number <= 1 ) {
		$('.prev-qrcode').hide();
	} else if( window.number >= 6 ) {
		$('.next-qrcode').hide();
	}

	// QR-Code details einblenden
	$('.qrcode-details li').hide();
	$('.qrcode-details li:nth-child(' + window.number + ')').fadeIn();
}
/* -------------------------------------------------------------
 *	Häufige Fragen Element
 * ------------------------------------------------------------- */
 
jQuery.fn.faq = function() { 
	$(".faqtabs .question").each(function(i ,el){
		var question = $(el);
		if(location.hash && location.hash.split('#')[1] == question.attr('name')){
			question.addClass('current')
			question.next('.answer').addClass('open')
		}else{
			question.removeClass('current')
			question.next('.answer').removeClass('open')
		}
				
		question.click(function(){
			var q=$(this);
			var a=q.next('.answer');
			
			if(q.hasClass('current')){
				// q.removeClass('current');
			}else{
				$('.answer.open').each(function(){
					if($(this).is(':visible')){
						$(this).slideUp(function(){$(this).removeClass('open')	});
					}else{
						$(this).removeClass('open')	
						$(this).css('display','none')
					}
					
					$(this).prev('.question').removeClass('current')
				})
				q.addClass('current');
				q.next('.answer').slideDown(function(){$(this).addClass('open')})
			}
			
		})
		
	});
}
