$(document).ready(function() {

//Test func
function c(temp) { console.log(temp); } 

/*************************/ 
/* NAVIGATION FUNCTIONS 
/*************************/
    var navigation = {
        init: function() {
            $('.nav ul .last div ul').width($('.nav ul .last').width());
            
            //MAKES ROOM FOR LOGOS IN LEFT COLUMN IN BOTTOM
            if($('.subnav').height() > $('.colB:first').height()) {
                $('.subnav').css('min-height', ($('.subnav').height() + $('.facebook-find').height() + $('.verisign').height()) + 'px');
            }
            $('.facebook-find, .verisign').css('visibility', 'visible');
        }
    }
    navigation.init();

/*************************/ 
/* GENEREL FUNCTIONS 
/*************************/  
     var generalFunctions = {
        init: function() {
            this.inputFocus();
            this.scrollToTopLink();
            this.popupCredits();
            this.newsletterSignup();
        },
        // CLEAR INPUT ON FUCUS
        inputFocus: function() {
            $('.clear-default').each(function(){
                var default_value = $(this).val();
                $(this).focus(function(){
                    if ($(this).val() == default_value) $(this).val('');
                });
                $(this).blur(function(){
                    if ($(this).val() == "") $(this).val(default_value);
                });
            });
        },
        // BACK TO TOP SCRIPT 
        scrollToTopLink: function() { 
            $('a.scrollTop').click(function(){
                $('html, body').animate({scrollTop: '0px'}, 300);
                return false;
            });            
        },
        popupCredits: function() {
            $('.credits').click(function(){
                html = '<html><head><title>Dwarf</title></head><body style="margin: 10px; text-align:center;"><a href="http://www.dwarf.dk" target="_blank" title="Dwarf"><img src="/images/backgrounds/credits.jpg" border=0 name=image></a></body></html>'; 
                popup = window.open('', 'image', 'toolbar=0, location=0, directories=0, menuBar=0, scrollbars=0, resizable=1, width=300, height=200');
                popup.document.open();
                popup.document.write(html);
            });
        },
				newsletterSignup: function () {
						$('#btn_newsletter').click(function () {
									// fields
								var email = $('#newslettersignup #email').val();
								
								$.post("/services/newsletter_signup.php", { e: email, a: "signup" }, function (data) {
									$('#newslettersignup #content').hide();
									$('#newslettersignup .outer').hide();
									$('#newslettersignup #btn_newsletter').hide();
									$('#newslettersignup #status').show().html(data);
									$('#newslettersignup #btn_news_return').show();
								});	
					 });
				 	 $('#btn_news_return').click(function () {
							  $('#newslettersignup #content').show();
							  $('#newslettersignup .outer').show();
							  $('#newslettersignup #btn_newsletter').show();
							  $('#newslettersignup #btn_news_return').hide();
							  $('#newslettersignup #status').hide().html(data);
					});	
				}
    }
    generalFunctions.init();

/*********************************/ 
/* FRONTPAGE SPECIFIC FUNCTIONS 
/*********************************/
    var frontpage = {
        //CONTROLS PLACEMENT OF BOXES 
        boxes: function() {
            var index = 0;
            $('.mFrontendBoxSmall, .mFrontendBoxWide, .mFrontendApologyBox').each(function(){
                if($(this).hasClass('mFrontendBoxSmall')) { index++; }
                if($(this).hasClass('mFrontendBoxWide')) { index = index + 2; }
                if($(this).hasClass('mFrontendApologyBox')) { index = index + 2; }
                if(index % 3 == 0) { $(this).attr('style', 'margin-right: 0 !important'); } 
                $(this).addClass('visible');
            });
        }
    }
    if($('.mFrontendBoxSmall').length != 0 || $('.mFrontendBoxWide').length != 0) { frontpage.boxes(); }

/*********************************/ 
/* AREA CAROUSEL 
/*********************************/
    var areaCarousel = {
        areaCarousel: function() {},
        init: function(domObj, runIt) {
            $this = this;
            this.domObj = domObj;
            this.active = 0;
            this.change = 1;
            this.max = $(domObj).find('.content li').length;
            /*this.timer;*/
            this.navigator = $(domObj).find('.indicator').length;
            this.rightLeftBtns = $(domObj).find('.right-btn, .left-btn').length;
            
            $(domObj).find('.content li:eq(0)').css({ 'z-index' : '5', 'display' : 'block' });
            $(domObj).find('.content li:not(li:eq(0))').css({ 'opacity': '0', 'zIndex' : '4' });
            if($('.poi-collection').length != 0)
                $(domObj).find('.poi-collection:eq(0)').css('display', 'block');             
            $(domObj).find('.product-wrapper:eq(0)').css('display', 'block');
            $(domObj).find('.product-wrapper').each(function(){
                $(this).find('.product:nth-child(3n)').css('margin-right', '0');
                $(this).find('.product:nth-child(3n+1)').css('clear', 'left');
            });
			$(domObj).find('.text-wrapper .text:eq(0)').css('display', 'block');

            if (this.max > 1) {
                
                if(this.navigator != 0) {
                    for( i=0; i < this.max; i++ ) {
                            (i == 0) ? $(domObj).find('.indicator').append('<span class="active"></span>') : $(domObj).find('.indicator').append('<span></span>') ;
                    }
                    this.interactIndicator();
                }
                
                if(this.rightLeftBtns != 0) {
                    $(this.domObj).find('.right-btn, .left-btn').css('display', 'block');
                    this.nextPrevNav();
                }
               
/*
                $(this.domObj).mouseover(function(){ clearInterval($this.timer); });
                $(this.domObj).mouseleave(function(){ $this.timeout(); });
                
                this.timeout();
*/
            }

        },
       /* timeout: function () {
            clearTimeout($this.timer);
            this.timer = setTimeout(function() { $this.interval(); }, 6000);
        },
        interval: function(){
            this.timer = setInterval(function() {
                $this.change = $this.active;
                $this.change++;
                if ($this.change == $this.max) {
                    $this.change = 0;
                }
                $this.changeActive();
            }, 6000);
        },*/
        changeActive: function() {
//            this.timeout();
            areaPOI.closeBubble();
            $(this.domObj).find('.content li:eq(' + this.active + ')').css('zIndex', '4');
            if($('.poi-collection').length != 0)
                $(this.domObj).find('.poi-collection:eq(' + $this.active + ')').css('display', 'none');
            $(this.domObj).find('.product-wrapper:eq(' + this.active + ')').css('display', 'none');
            $(this.domObj).find('.text-wrapper .text:eq(' + this.active + ')').css('display', 'none');
            $(this.domObj).find('.content li:eq(' + this.change + ')').css('zIndex', '5');
            $(this.domObj).find('.product-wrapper:eq(' + this.change + ')').css('display', 'block');
            $(this.domObj).find('.text-wrapper .text:eq(' + this.change + ')').css('display', 'block');
            $(this.domObj).find('.content li:eq(' + this.active + ')').stop().animate({ opacity: 0 }, 1000, function() { 
                $(this).css('display', 'none');
                if($('.poi-collection').length != 0)
                    $($this.domObj).find('.poi-collection:eq(' + $this.change + ')').css('display', 'block'); 
            });
            $(this.domObj).find('.content li:eq(' + this.change + ')').css('display', 'block').stop().animate({ opacity: 1 }, 1000);

            if($this.navigator != 0)
                this.changeIndicator();
            
            this.active = this.change;
        },
        changeIndicator: function() {
                $(this.domObj).find('.indicator span:eq('+ this.change +')').addClass('active').siblings().removeClass('active');
        },
        interactIndicator: function() {
            $(this.domObj).find('.indicator span').mousedown(function(){
                if($(this).index() != $this.active) {
                    $this.change = $(this).index();
                    $this.changeActive();
                }
            });         
        },
        nextPrevNav: function() {
            $(this.domObj).find('.right-btn').mousedown(function(){
                $this.change = $this.active;
                $this.change++;
                if ($this.change == $this.max) 
                    $this.change = 0;
                
                $this.changeActive();
            });

            $(this.domObj).find('.left-btn').mousedown(function(){
                clearInterval($this.timer);
                $this.change = $this.active;
                
                if ($this.change == 0) {
                    $this.change = $this.max-1;
                    $this.changeActive();
                } else {
                    $this.change--;
                    $this.changeActive();             
                }
            });            
        }
    }
    
    if($('.mFrontendArea').length != 0) { areaCarousel.init($('.mFrontendArea .carousel')); }
   
    //Points of interest on the carousel images
    var areaPOI = {
        init: function() {
            this.mouseInit();
            this.bindClose(); 
        },
        mouseInit: function() {
            var xPos, yPos, headline, link, text, index, oldIndex;

            $('.poi-area .poi').live('click', function(event){
                event.stopPropagation();
                index = $(this).index();
                if(index == oldIndex && $('.poi-area .bubble').is(':visible')) {
                    areaPOI.closeBubble();
                } else {
                    event.stopPropagation();
                    xPos = $(this).position().left;
                    yPos = $(this).position().top;
                    headline = $(this).find('.headline').html();
                    link = $(this).find('.link').html();
                    text = $(this).find('.text').html();
                    
                    $('.poi-area .bubble').find('h3').html(headline)
                    .end().find('.link').html(link)
                    .end().find('.text').html(text);
                    
                    if(xPos < 402 && yPos < 237) {
                        $('.poi-area .bubble').find('div:eq(0)').attr('class', 'top-left')
                        .end().find('div:eq(2)').attr('class', 'bottom')
                        .end().find('.middle').css('padding-bottom', '0')
                        .end().css({ 'left' : xPos-10, 'top' : yPos+40 });
                    }
                    if(xPos > 401 && yPos < 237) {
                        $('.poi-area .bubble').find('div:eq(0)').attr('class', 'top-right')
                        .end().find('div:eq(2)').attr('class', 'bottom')
                        .end().find('.middle').css('padding-bottom', '0')
                        .end().css({ 'left' : xPos-164, 'top' : yPos+40 });
                    }
                    if(xPos < 402 && yPos > 236) {
                        $('.poi-area .bubble').find('div:eq(0)').attr('class', 'top')
                        .end().find('div:eq(2)').attr('class', 'bottom-left')
                        .end().find('.middle').css('padding-bottom', '10px')
                        .end().css({ 'left' : xPos-10, 'top' : yPos-132 });  
                    }
                    if(xPos > 401 && yPos > 236) {
                        
                        $('.poi-area .bubble').find('div:eq(0)').attr('class', 'top')
                        .end().find('div:eq(2)').attr('class', 'bottom-right')
                        .end().find('.middle').css('padding-bottom', '10px')
                        .end().css({ 'left' : xPos-164, 'top' : yPos-126 });
                    }
                    
                    //Cufon.replace('.bubble h3, .bubble p', {fontFamily: 'Antenna'});
                    
                    $('.poi-area .poi .icon').removeClass('active');
                    $(this).find('.icon').addClass('active');
                    
                    $('.poi-area .bubble').css('display', 'block');
                    areaPOI.bindClose();
                } 

                oldIndex = index;
            });
        },
        bindClose: function() {
 			if($.browser.msie && $.browser.version == 8) {
        		$('.navigation, .poi-area .bubble, .poi-area').live('click', function(event){
                	event.stopPropagation();
                	areaPOI.closeBubble();
            	}); 	
			} else {
				$('.navigation, .poi-area .bubble, .poi-area, .carousel').live('click', function(event){
                	event.stopPropagation();
                	areaPOI.closeBubble();
            	});
         	}
        },
        closeBubble: function() {
            $('.poi-area .bubble').css('display', 'none');
            $('.poi-area .poi .icon').removeClass('active');
            $('.navigation, .poi-area .bubble, .poi-area').die('click');           
        }
    }
    
    if($('.poi-collection').length != 0) { areaPOI.init(); }

/*********************************/ 
/* APOLOGY FUNCTIONS 
/*********************************/
       
    var apologyCarousel = {
        init: function(domObj) {
            $thisA = this;
            this.domObj = domObj;
            this.active = 0;
            this.change = 1;
            this.max = $(this.domObj).find('li').length;
            this.timer;
            this.ddTimer;
            
            $(this.domObj).find('li:eq(0)').css({ 'z-index' : '5', 'display' : 'block' });
            $(this.domObj).find('li:not(li:eq(0))').css({ 'opacity': '0', 'zIndex' : '4' });

/*
            if (this.max > 1) {
                $(this.domObj).mouseover(function(){ clearInterval($thisA.timer); });
                $(this.domObj).mouseleave(function(){ $thisA.timeout(); });
                this.timeout();
            }
*/
            
            this.dropDown();
        },
        timeout: function () {
            clearTimeout($thisA.timer);
            this.timer = setTimeout(function() { $thisA.interval(); }, 6000);
        },
        interval: function(){
            this.timer = setInterval(function() { 
                $thisA.change = $thisA.active;
                $thisA.change++;
                if ($thisA.change == $thisA.max) {
                    $thisA.change = 0;
                }
                $thisA.changeActive();
            }, 6000); 
        },
        changeActive: function() {
//            this.timeout();
            $(this.domObj).find('li:eq(' + this.active + ')').css('zIndex', '4');
            $(this.domObj).find('li:eq(' + this.change + ')').css('zIndex', '5');
            $(this.domObj).find('li:eq(' + this.active + ')').stop().animate({ opacity: 0 }, 1000, function() { $(this).css('display', 'none'); });
            $(this.domObj).find('li:eq(' + this.change + ')').css('display', 'block').stop().animate({ opacity: 1 }, 1000);

            this.active = this.change;
        },
        dropDown: function() {
            Cufon.replace('.dropdown-con h1', {fontFamily: 'Adobe Garamond Pro'});
            Cufon.replace('.dropdown-con h2, .mFrontendApology .inner-dd, .mFrontendApologyBox .inner-dd, .mFrontendApology .dropdown-con a, .mFrontendApologyBox .dropdown-con a', {fontFamily: 'Antenna'});
            
            $('.dropdown-con h1, .dropdown-con h2, .dropdown-con ul').css('display', 'block');
            
            $(this.domObj).next('.dropdown-con').find('.outer-dd').click(function(){ 
                $(this).next('.outer-dd-block').css('display', 'block');
                $(this).next('.outer-dd-block').css('top', -$(this.domObj).find('.dropdown-con .inner-dd-block').height()-100);
            }); 
            
            $('.dropdown-con .outer-dd-block').mouseleave(function(){
                $('.dropdown-con .outer-dd-block').css('display', 'none');
            });   
        }, 
        timeout: function () {
            clearTimeout($thisA.ddTimer);
            this.ddTimer = setTimeout(function() { $thisA.interval(); }, 500);
        },
        interval: function(){
            this.ddTimer = setInterval(function() {
                $('.dropdown-con .outer-dd-block').css('display', 'none');
                clearTimeout($thisA.ddTimer);
            }, 500);
        }
    }       
       
    if($('.dropdown-con').length != 0) { 
        apologyCarousel.init($('.apology-carousel .carousel')); 
        apologyCarousel.init($('.mFrontendApologyBox .drop-next'));
        apologyCarousel.init($('.mFrontendArea .drop-next-area'));
         
    }
    
/*********************************/ 
/* SEARCHPAGE NAVIGATION
/*********************************/    

    var searchPage = {
        init: function(domObj) {
            searchPage.domObj = domObj
            searchPage.current = 1;
            searchPage.num = $(domObj).find('ul').length;
            
            if(searchPage.num > 1) {
                $(domObj).find('.next').css('display', 'block');
                this.mouseInit();
            }
        },
        pageTurn: function(direction) {
            if(direction) {
                searchPage.dis = searchPage.current * -$(searchPage.domObj).width();
                searchPage.current++;
            } else {
                searchPage.dis = searchPage.dis + $(searchPage.domObj).width();
                searchPage.current--;
            }
            
            if(searchPage.current == 1) { $(searchPage.domObj).find('.back').css('display', 'none'); } 
            else if(searchPage.current > 1) { $(searchPage.domObj).find('.back').css('display', 'block'); }
            if(searchPage.current == searchPage.num) { $(searchPage.domObj).find('.next').css('display', 'none'); } 
            else if(searchPage.current < searchPage.num) { $(searchPage.domObj).find('.next').css('display', 'block'); }
            
            $(searchPage.domObj).find('.page-search-outer').animate({
                marginLeft: searchPage.dis
            }, 500);
        },
        mouseInit: function() {
            $(searchPage.domObj).find('.next').click(function() { searchPage.pageTurn(true); });
            $(searchPage.domObj).find('.back').click(function() { searchPage.pageTurn(false); });
        }
    }
    
    if($('.mFrontendPageSearch').length != 0) { searchPage.init($('.mFrontendPageSearch')); }



/*****************************/
/* POPUP CAMPAIGN WINDOW
/*****************************/


    $.fn.popOpen = function(){
        
        popID = $(this).attr('rel'); //Get Popup Name
        popURL = $(this).attr('href'); //Get Popup href to define size
        
        //Pull Query & Variables from href URL
        query= popURL.split('?');
        dim= query[1].split('&');
        popWidth = dim[0].split('=')[1]; //Gets the first query string value
        
        
        //Fade in the Popup and add close button
        $('#' + popID).fadeIn().css({ 'width': Number( popWidth ) }).prepend('<a href="#" class="close"><img src="images/close_pop.png" class="btn_close" title="Luk vindue" alt="Close" /></a>');
        
        //Define margin for center alignment (vertical + horizontal) - we add 80 to the height/width to accomodate for the padding + border width defined in the css
        var popMargTop = ($('#' + popID).height() + 80) / 2;
        var popMargLeft = ($('#' + popID).width() + 80) / 2;
        
        //Apply Margin to Popup
        $('#' + popID).css({ 
            'margin-top' : '-250px',
            'margin-left' : '-320px'
        });
        
        //Fade in Background
        $('body').prepend('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
        $('#fade').css({'filter' : 'alpha(opacity=40)'}).fadeIn(); //Fade in the fade layer 
        
    };

    //When you click on a link with class of poplight and the href starts with a # 
    $('a.poplight[href^=#]').click(function() {
        $(this).popOpen(); //Run popOpen function on click
        return false;
    });
    
    $('a.poplight[href=#?w=350]').popOpen(); //Run popOpen function once on load
    
    //Close Popups and Fade Layer
    /*$('a.close').live('click', function() { //When clicking on the close or fade layer...
        $('.popup_block').hide(); //fade them both out
        return false;
    });*/
	
	//Close Popups and Fade Layer
    $('a.close, #fade').live('click', function() { //When clicking on the close or fade layer...
        $('#fade , .popup_block').fadeOut(); //fade them both out
        $('#fade').remove();
        return false;
    });










});


