var heightOfImagePaddedWithBar = 308;
$(document).ready(function() {

    // campagin site pad-down
    if ($(".campaign_site").length > 0) { setCampaginSite(); }

    // googlemaps 
    if ($(".okmap").length > 0) { LoadOKGoogleMap(); }

    // frontpage slider
    if ($("#slideshow").length > 0 && $('.text').size() > 1) {
        $("#slideshow").slideshow(
			    {
			        duration: 5500,
			        slideUpSpeed: 600,
			        slideDownSpeed: 600,
			        fadeInSpeed: 625,
			        fadeOutSpeed: 625
			    }
		    );
    }

    // only 1 slide - then hide the slide nav 
    if ($('.text').size() == 1) {
        $('.navi li').css("display", "none");
    }

    // share with friend button
    if ($("a.btn_tip").length > 0) {
        $("a.btn_tip").fancybox({
            'autoDimensions': false,
            'frameHeight': 245,
            'frameWidth': 358,
            'hideOnContentClick': false,
            'hideOnOverlayClick': false,
            'onClosed': function() { closeFancyTip(); }
        });
    }

    // Listepris buttons
    if ($(".listepris_pop_link").length > 0) {
        $("a.listepris_pop_link").fancybox({
            'autoDimensions': false,
            'frameHeight': 245,
            'frameWidth': 358,
            'hideOnContentClick': false,
            'hideOnOverlayClick': true
        });
    }

    // Listepris buttons
    if ($(".guide").length > 0) {
        if ($.cookie("MINGUIDE") == "CLOSED") {
            $('.guide .tabs').css("display", "none");
            $('.MinOKGuide .guide').addClass('guide_closed');
            $('.guide .header a').addClass('guide_closed');
        }
    }

    // print button
    $('.btn_print').click(function() {
        window.print();
        return false;
    });

    // empty on focus and blur function for the search field
    $('.input_txt').each(function() {
        var default_value = this.value;
        $(this).focus(function() {
            if (this.value == default_value) {
                this.value = '';
            }
        });
        $(this).blur(function() {
            if (this.value == '') {
                this.value = default_value;
            }
        });
    });

    // position the content so the sharepoint menu is visible
    if (($(".sharepoint_bar").length > 0) && ($("#zz1_SiteActionsMenuMain").length > 0)) {
        actualHeight = $(".sharepoint_bar").height();
        actualHeightWrapper = actualHeight + 'px';
        if ($(".frontpage").length > 0) {
            actualHeightContent = actualHeight + 345 + 'px 0px 0px 0px';
            actualHeightSlide = actualHeight + 365 + 'px';
            $('.position_wrapper_frontpage').css({ height: actualHeightSlide });
        } else {

            actualHeightContent = heightOfImagePaddedWithBar + actualHeight + 'px 0px 0px 0px';
            $('#top_banner_wrapper').css({ top: actualHeightWrapper });
        }
        $('#top_con').css({ top: actualHeightWrapper });
        $('#slideshow').css({ top: actualHeightWrapper });
        $('#content_con').css({ margin: actualHeightContent });
    }

    // action button placement
    if ($('.new_user').length > 0) {
        buttonWidth = (($('.action_btn_text').width()) + 14) + 'px';
        $('.action_btn').css({ width: buttonWidth });
    }

    // forms help btn and close btn
    $(function() {
        var helpBtn = function() { $(this).next('div').css("display", "block") };
        $('.help_btn').click(helpBtn);

        var helpCloseBtn = function() { $(this).parent().css("display", "none") };
        $('.help_close').click(helpCloseBtn);

        var closeErrorBtn = function() { $(this).next('span').css("display", "none"); $(this).parent('.form_validator').css("display", "none"); };
        $('.form_validator_close').click(closeErrorBtn);

        $('.guide .header a').click(showGuide);

    });

    // Activate the download file tracker
    //var filetypes = /\.(zip|exe|pdf|doc*|xls*|ppt*|jpg|gif|png|mov|flv|wma|mp3|kml|csv|ov2)$/i;

    //$('a').each(function() {
    //    var href = $(this).attr('href');

    //    if ((href.match(/^https?\:/i)) && (!href.match(document.domain))) {
    //        $(this).click(function() {
    //            var extLink = href.replace(/^https?\:\/\//i, '');
    //            pageTracker._trackEvent('External', 'Click', extLink);
    //        });
    //    }
    //    else if (href.match(/^mailto\:/i)) {
    //        $(this).click(function() {
    //            var mailLink = href.replace(/^mailto\:/i, '');
    //            pageTracker._trackEvent('Email', 'Click', mailLink);
    //        });
    //    }
    //    else if (href.match(filetypes)) {
    //        $(this).click(function() {
    //            var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined;
    //            var filePath = href.replace(/^https?\:\/\/(www.)ok\.dk\//i, '');
    //            pageTracker._trackEvent('Download', 'Click - ' + extension, filePath);
    //        });
    //    }
    //});

});

// Min OK guide slider 
function showGuide() {
    if ($('.guide .tabs').is(':visible')) {
        $('.guide .tabs').slideUp('slow', function() { $('.MinOKGuide .guide').toggleClass('guide_closed'); });
        pageTracker._trackEvent('MinOKGuide', 'Lukket');
        $('.guide .header a').addClass('guide_closed');
        $.cookie("MINGUIDE", "CLOSED", { expires: 180 });
    }
    if (!($('.guide .tabs').is(':visible'))) {
        $('.guide .tabs').slideDown('slow', function() { $('.MinOKGuide .guide').toggleClass('guide_closed'); });
        pageTracker._trackEvent('MinOKGuide', 'Åbnet');
        $('.guide .header a').removeClass('guide_closed');
        $.cookie("MINGUIDE", "OPEN", { expires: 180 });
    }
}

// Min OK guide Item slider
function showGuideItem(itemId, linkId, title) {
    $('.tabcontent li:visible').fadeOut('normal', function() { $('#' + itemId).fadeIn(); });
    $('.tabchooser li').removeClass('activeGuideLink');
    $('#' + linkId).addClass('activeGuideLink');
    pageTracker._trackEvent('MinOKGuide', 'Vis', title);
}

//function for campaign site image padding
function setCampaginSite() {
    if ($('#top_banner_img img').length > 0) {
        heightOfImage = ($('#top_banner_img img').height()) + "px";
        heightOfImagePadded = (($('#top_banner_img img').height()) + 98) + "px";
        heightOfImagePaddedWithBar = (($('#top_banner_img img').height()) + 98);
        $('#top_banner_wrapper').css('height', heightOfImagePadded);
        $('#top_banner').css('height', heightOfImagePadded);
        $('#top_banner_img').css('height', heightOfImage);
        $('#content_con').css('margin-top', heightOfImagePadded);
    }
}


(function($) {

    $.fn.slideshow = function(option) {
        var config = {
            current: 0, 		// current selected index
            max: 0, 		// how many 
            duration: 2000, 	// speed between animation
            slideUpSpeed: "fast", //
            slideDownSpeed: "fast", //
            fadeInSpeed: "fast", //
            fadeOutSpeed: "fast", //
            easing: null, 	// not in use
            callback: null		// not in use
        }
        config = $.extend(config, option);
        var self = this,
			timer = null;

        $(self).find(".text").hide().eq(0).slideDown(0);
        $(self).find(".img img").hide().eq(0).fadeIn(0);
        $(self).find(".navi ul li:first-child a").addClass("selected");

        config.current = 0;
        config.max = self.find("div.text").length;

        doAnimate = function(current, next) {
            /*$(self).find(".text").eq(current).slideUp(config.slideUpSpeed,
            function() {
            $(self).find(".text").eq(next).slideDown(config.slideDownSpeed);
            });
            $(self).find(".text").eq(current).fadeOut(config.fadeOutSpeed);
            $(self).find(".text").eq(next).fadeIn(config.fadeInSpeed);*/
            setTimeout(function() {
                $(self).find(".navi ul li a").eq(current).removeClass("selected");
                $(self).find(".navi ul li a").eq(next).addClass("selected");
                $(self).find(".text").eq(current).hide();
                $(self).find(".text").eq(next).show();
            }, 300);

            $(self).find(".img img").eq(current).fadeOut(config.fadeOutSpeed);
            $(self).find(".img img").eq(next).fadeIn(config.fadeInSpeed);


        }

        $(self).find(".navi ul li a").click(
		function() {
		    $(this).stop();
		    clearTimeout(timer);
		    var next = $(self).find(".navi ul li a").index(this);
		    doAnimate(config.current, next);
		    config.current = next;
		    return false;
		}
	)

        start = function() {
            timer = setTimeout(function() {
                var next = config.current + 1;
                if (next >= config.max) {
                    next = 0;
                }
                doAnimate(config.current, next);
                config.current = next;
                start();
            }, config.duration);
        }

        start();
        return self;

    };

})(jQuery);