var isApple = false;
var isAnimating = false;
var $cover;
$(document).ready(function () {
    controllPage();
});
function controllPage() {
    $cover = $('#cover');
    if (navigator.userAgent.indexOf('AppleWebKit') > -1) {
        isApple = true;
    }
    else if (navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod') {
        isApple = true;
        $("#content").css("height", "550px");
    }
    $('.imagelist-content').each(function (index) {
        if (index % 2 == 1) {
            $(this).addClass('nopadding');
        }
    });
    $('#menu li.hotel:first-child').css('margin-left', '0');
    $('.bloggers li').each(function (index) {
        if ((index + 1) % 3 == 0) {
            $(this).addClass('no-right-padding');
            $(this).after('<li class="breakme"></li>');
        }
    });
    $('.bloggers li').parent().css('display', 'block');
    $('ul.con-page-top li').each(function (index) {
        if ((index + 1) % 2 == 0) {
            $(this).addClass('no-right-padding');
        }
    });
    $('.handle-content').each(function (index) {
        if (index > 0) {
            $(this).css('display', 'none');
        }
    });
    $(window).resize(function () {
        if (navigator.userAgent.indexOf('MSIE 7.') > -1) {
            $cover.width(($(document).width() > $(window).width() ? $(document).width() : $(window).width()));
            $cover.height(($(document).height() > $(window).height() ? $(document).height() : $(window).height()));
        }
        else {
            $cover.width(($(document).width() > $(window).width() ? $(document).width() : $(window).width()));
            $cover.height(($(document).height() > $(window).height() ? $(document).height() : $(window).height()));
        }
    });
    enableFooterNav();
    var selectedHitClass = false;
    $('#handle-category a').each(function (index) {
        if ($(this).hasClass('selected')) {
            var selectClass = $(this).attr('class').match("menu-[1-9]");
            toggleQuickMenuTab(index, selectClass);
            selectedHitClass = true;
        }
    });
    if (!selectedHitClass) {
        toggleQuickMenuTab(0, "menu-2");
    }
    $('ul.pagination').after($('a.next'));
    $('.multicolumn .bodytext').columnize({ columns: 2 });
    Cufon.replace('.multicolumn h2', { fontFamily: 'helveticabold' });
    Cufon.replace('.related h2', { fontFamily: 'maqui' });
    Cufon.replace('#handle-category span.selected a', { fontFamily: 'maqui' });
    //Ändrar typsnitt på blockquotes i bodytexten
    Cufon.replace('.bodytext blockquote, .bodytext p q, .bodytext h3', { fontFamily: 'helveticacond' });
    $(window).bind('orientationchange', function (e) {
        fixOrientation(e);
    });
    $('#handle').css('position', 'fixed');
    $('#footer').show();
    $('#wrapper.startpage .get-me-down h2').each(function () {
        $(this).css('margin-top', '0');
        $(this).css('margin-bottom', '0');
        var oldWidth = $(this).css('width');
        $(this).css('max-height', '90px');
        $(this).css('width', oldWidth);
    });
    $('#wrapper.startpage .get-me-down h3').each(function () {
        $(this).css('margin-top', '0');
        $(this).css('margin-bottom', '0');
        var oldWidth = $(this).css('width');
        $(this).css('max-height', '60px');
        $(this).css('width', oldWidth);
    });
    $('#wrapper.startpage .get-me-down h2:first-child cufon,#wrapper.startpage .get-me-down h3:first-child cufon').each(function () {

        switch ($(this).css('height')) {
            case '92px':
                $(this).css('height', '75px');
                break;
            case '30px':
                $(this).css('height', '26px');
                break;
            case '60px':
                $(this).parent().css('padding-top', '10px');
                $(this).css('height', '44px');
                break;
            case '42px':
                if (navigator.userAgent.indexOf('MSIE 8.') > -1) {
                    $(this).css('height', '30px');
                }
                else {
                    $(this).css('height', '26px');
                }
                break;
            default:
                break;
        }
    });
    $('.all-col').bind('click', function (e) {
        if ($(e.target).parents('h2,h3,span').length == 0) {
            location.href = './Innehall/';
        }
    });
    $('#toolssocial a').each(function () { $(this).attr('target', '_blank'); });
}
function fixOrientation(e) {
    $('footer').css('top','921');
}
function enableFooterNav() {
    if (!quickmenutabactive) {
        quickmenutabactive = 'menu-2';
    }
    $('.footerprev').click(function (e) {
        if (isAnimating) {
            e.preventDefault();
            return false;
        }
        var parent = $(this).parent();
        var container;
        container = $('.handle-content.' + quickmenutabactive);
        var ul = $('ul', container);
        if (ul.position().left < 0) {
            if (isApple) {
                ul.css({ '-webkit-transform': 'translate3d(' + (ul.position().left + 798) + 'px,0px,0px)' });
                setTimeout(activateButtons, 1000);
            }
            else {
                ul.animate({ 'left': (ul.position().left + 798) + 'px' }, 800, activateButtons);
            }
        }
        else if (ul.position().left == 0 && ul.width() > 798) {
            var theEnd = 0 - ((Math.ceil(ul.width() / 798) - 1) * 798);
            if (isApple) {
                ul.css({ '-webkit-transform': 'translate3d(' + theEnd + 'px,0px,0px)' });
                setTimeout(activateButtons, 1000);
            }
            else {
                ul.animate({ 'left': theEnd + 'px' }, 800, activateButtons);
            }
        }
        else {
            if (isApple) {
                ul.css({ '-webkit-transform': 'translate3d(0px,0px,0px)' });
                setTimeout(activateButtons, 1000);
            }
            else {
                ul.animate({ 'left': '0px' }, 800, activateButtons);
            }
        }
        isAnimating = true;
        e.preventDefault();
        return false;
    });
    $('.footernext').click(function (e) {
        if (isAnimating) {
            e.preventDefault();
            return false;
        }
        var parent = $(this).parent();
        container = $('.handle-content.' + quickmenutabactive);
        var ul = $('ul', container);
        if (Math.abs(ul.position().left) < (ul.width() - 798)) {
            if (isApple) {
                ul.css({ '-webkit-transform': 'translate3d(' + (ul.position().left - 798) + 'px,0px,0px)' });
                setTimeout(activateButtons, 1000);
            }
            else {
                ul.animate({ 'left': (ul.position().left - 798) + 'px' }, 800, activateButtons);
            }
        }
        else {
            if (isApple) {
                ul.css({ '-webkit-transform': 'translate3D(0px,0,0)' });
                setTimeout(activateButtons, 1000);
            }
            else {
                ul.animate({ 'left': '0px' }, 800, activateButtons);
            }
        }
        isAnimating = true;
        e.preventDefault();
        return false;
    });
}
function activateButtons() {
    isAnimating = false;
}
var quickmenuvisible = false;
function toggleQuickMenu() {
    if (navigator.platform == 'iPhone' || navigator.platform == 'iPod') {
        return;
    }
    var $cover = $('#cover');
    var pixels = '218px';
    if (navigator.appVersion.indexOf("MSIE 7.") != -1) {
        pixels = '223px';
    }
    if(!quickmenuvisible) {
    	quickmenuvisible = true;
    	if (navigator.userAgent.indexOf('MSIE') > -1) {
    	    $('html').css('overflow-x', 'hidden');
            $cover.width(($(document).width() > $(window).width() ? $(document).width() : $(window).width()));
            $cover.height(($(document).height() > $(window).height() ? $(document).height() : $(window).height()));
	    }
	    else {
	        $cover.width($(document).width());
            $cover.height($(document).height());
	    }
		$('#handle').css({'top':'auto'});
		$('#handlebar').removeClass('closed');
		$('#handlebar').addClass('open');
	    $('#handle').animate({ bottom: '+='+pixels }, 'fast', function () {
		    $cover.css('display', 'block');
		});
	}
	else {
	    quickmenuvisible = false;
	    $('#handlebar').removeClass('open');
	    $('#handlebar').addClass('closed');
        $('#handle').animate({ bottom: '-='+pixels }, 'fast', function () {
            $cover.css('display', 'none');
            if (navigator.userAgent.indexOf('MSIE') > -1) {
                $('html').css('overflow-x', 'auto');
            }
        });
	}
}
var quickmenutabactive;
function toggleQuickMenuTab(indexToActivate,selector) {
    quickmenutabactive = selector;
    $('.handle-content').each(function () {
        $(this).css('display', 'none');
    });
    $('.handle-content.' + selector).css('display','');
    $('#handle-category a').each(function (index) {
        if (index == indexToActivate) {
            $(this).addClass('selected');
            $(this).parent().addClass('selected');
            Cufon.replace('#handle-category span.selected a', { fontFamily: 'maqui' });
        } else {
            $(this).removeClass('selected');
            $(this).parent().removeClass('selected');
            Cufon.replace('#handle-category a', { fontFamily: 'maqui' });
        }
    });
}
function hideOverlay() {
    $('#cover').hide();
    quickmenuvisible = true;
    toggleQuickMenu();
}

function toggleDetailedArticle(toggleImg) {
	$exp = $(toggleImg).parents('.ArticleListArticle');
	$('.ExpandCollapse', $exp).toggleClass('Collapse');
    $('.ShortIntro', $exp).slideToggle('fast');
    $('.Intro', $exp).slideToggle('fast');
}
function ShowModalDialog(id,url) {
    if (navigator.platform == 'iPhone' || navigator.platform == 'iPod' || navigator.platform == 'Linux armv7l') {
        window.open(url);
        return;
    }
    $(id).dialog({draggable: false,resizable: false,modal: true,height: 'auto',width: 'auto',open: function () { $('iframe', $(this)).attr('src', url);},close: function () {$('iframe', $(this)).attr('src', '');var $cache = $(this).html();$(this).html("");$(this).html($cache);}});
}
