var UV = window.UV || {};

UV.Dropdown = {
    init: function() {
        var $ul = null;
        // Find all li elements that has an UL child.
        $('#local-navigation > li:has(ul)')
		.hover(
			function() {
			    $(this).css({ position: 'relative' }).find('.layer').show();
			},
			function() {
			    $(this).css({ position: 'static' }).find('.layer').hide();
			}
		).each(function() {
		    $(this).find('ul:eq(0)').show().prepend($('<li></li>').addClass('topic').append($(this).find('a:first').clone()))
			.wrap('<div class="layer"></div>').find('ul').show();
		});
        
        // Hover effect on all children
        $('#local-navigation li li').hover(
			function() {
			    $(this).find('a:eq(0)').addClass('hover');
			},
			function() {
			    $(this).find('a').removeClass('hover');
			}
		);
    }
};


/**
 * We use the initCallback callback
 * to assign functionality to the controls
 */
var CarouselHelper = {
	init: function(carousel) {
		carousel.selected=1;
		jQuery('.carousel_1_control a').bind('click', function() {    	
		    carousel.scroll(this.className);
		    return false;        
		}); 
		jQuery('#box_carousel_next').bind('click', function() {
		    carousel.next(); 
		    return false;
		}); 
		jQuery('#box_carousel_prev').bind('click', function() {
		    carousel.prev();
		    return false;
		});
	},
	Callbacks: {
		VisibleIn: {
			beforeAnimation: function(carousel,element,i,status) {
				var idx = carousel.index(i, carousel.options.size);
				carousel.add(i,carousel.get(idx).html());
			},
			afterAnimation: function(carousel,element,index,status) {
				var idx = carousel.index(index, carousel.options.size);
				carousel.selected=idx;
			}
		},
		visibleOut: function(carousel, item, i, state, evt) {
			if (i>carousel.options.size || i<0){
		    	carousel.remove(i);
		   	}
		}
	}
};

var Cookies = {
    testCookieName : "CookiesIsEnabledCookie",
    erase : function(name) {
        this.set(name, "" , -1);
    },
    get : function(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for(var i = 0; i < ca.length; i++) {
            var c = ca[i];
            
            while (c.charAt(0) == ' ') 
                c = c.substring(1, c.length);
            
            if (c.indexOf(nameEQ) == 0) 
                return c.substring(nameEQ.length, c.length);
        }
        return null;    
    },    
    isEnabled : function() {
        var c = null;
        
        this.set(this.testCookieName, "cookie", 0);
        c = this.get(this.testCookieName);
        this.erase(this.testCookieName);

        return c != null;    
    },
    set : function(name, value, days) {
        var expires = "";
        
        if (days) {
            var date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            expires = "; expires=" + date.toGMTString();
        } 
            
        document.cookie = name + "=" + value + expires + "; path=/";
    }
}


UV.Campaign = {
	cookieName: 'UVCampaign',
	cookieValue: 'visited',
	days: 1, // Cookie expires in 24h
	writeCookie: function() {
		var date = new Date();
		date.setTime(date.getTime()+(UV.Campaign.days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
		document.cookie = UV.Campaign.cookieName+"="+UV.Campaign.cookieValue+expires+"; path=/";
	},
	readCookie: function() {
		var nameEQ = UV.Campaign.cookieName + '=';
		var ca = document.cookie.split(';');
		
		for(var i=0; i<ca.length; i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
				// We have a match
				if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		// Cookie doesn't exist
		return null;
	},
	init: function() {
		// Browser must have cookies enabled
		if(!Cookies.isEnabled()) {
			return;
		}
		if (UV.Campaign.readCookie() !== null || window.location.pathname.indexOf('Foretag') !== -1) {
			// The visitor has seen the campaign.
			return;
		}
		// Make sure that the campaign exists in the DOM
		// before continuing.
		if ($('.fn_splashtrigger').length > 0) {
			// Setup fancybox
			$('.fn_splashtrigger').fancybox({
				overlayColor: '#000',
				overlayOpacity: '0.7',
				width: 470,
				height: 450,
				autoDimensions: false,
				showCloseButton: false,
				modal: true,
				onStart: function(){
					// Set a cookie and prevent the dialog from appearing for 24h
					UV.Campaign.writeCookie();
				}
			});
			$('.fn_splashclose').click(function() {
				$.fancybox.close();
			});
			// This is a workaround since fancybox lacks a load event,
			// in other words, fancybox can only be triggered by a click.
			$(".fn_splashtrigger").trigger('click');
		}			
	}
};

// Set on window load since we must make sure
// that all images are present in the DOM.
$(window).load(function() {
	UV.Campaign.init();
});

$(document).ready(function() {

    $('#startPageWrapper #imgDiv1 a').hover(
        function(e) {
            $('#startPageWrapper #hiddenText1').css('display', 'block');
        },
        function(e) {
            $('#startPageWrapper #hiddenText1').css('display', 'none');
        }
    );
    $('#startPageWrapper #imgDiv2 a').hover(
        function(e) {
            $('#startPageWrapper #hiddenText2').css('display', 'block');
        },
        function(e) {
            $('#startPageWrapper #hiddenText2').css('display', 'none');
        }
    );
	
	if ($('#carousel').length > 0) {
		$('#carousel').jcarousel({
			vertical: true,
			scroll: 3,
			wrap: 'circular',
			initCallback: CarouselHelper.init,
			itemVisibleInCallback: {
				onBeforeAnimation: CarouselHelper.Callbacks.VisibleIn.beforeAnimation,
				onAfterAnimation: CarouselHelper.Callbacks.VisibleIn.afterAnimation
			},
			itemVisibleOutCallback: CarouselHelper.Callbacks.visibleOut
		});
	}

    $('.fn-print').click(function() {
        print();
    }).css({
        cursor: 'pointer'
    });


    $('.collapsed-list h2 a, .collapsed-list h3 a').not('.collapsed-list ul li h3 a').click(function(e) {
        $(this).parent().eq(0).toggleClass('expanded').next('div').toggle();

    });
    $('.collapsed-list h2 a').click(function(e) {
        e.preventDefault();
    });

    $('a:not(div.info a, ul.collapsed-list h2 a, div.bottomInternalLink a, div.puff a)').linkicons({
        linkCss: {
            paddingRight: '22px',
            paddingBottom: '1px',
            backgroundPosition: '100% 1px'
        },
        iconPath: '/cui/img/icons/'
    });

    //
    $('div.info h3 a.external-link, div.faq-answer a').linkicons({
        linkCss: {
            paddingRight: '22px',
            paddingBottom: '1px',
            backgroundPosition: '100% 1px'
        },
        iconPath: '/cui/img/icons/'
    });

    $('.fn_flash').each(function() {
        var $t = $(this);
        $t.flash({
            swf: $t.html(),
            params: {
                play: true,
                loop: true,
                wmode: 'transparent'
            }
        });
    });

    UV.Dropdown.init();

    //$('#global-navigation-wraper').width(570 - $('#selected-site').width()); // fix for global navigation
});

