// REDIRECT MOBILE DEVICES AUTOMATICALLY
if (screen.width <= 480) {
document.location = "http://m.the-best-designs.com";
}

(function ($, window, document, undefined) {

    var debug = false;
    if (typeof console != "undefined") {
        debug = true;
    }

    function log(msg) {
        if (debug) { console.log(msg); }
    }
    
    var recentWork = [
        {
            img: "skanderborg-ungdomsskole.jpg",
            desc: "jQuery, css, html",
            linkText: "Skanderborg Ungdomsskole",
            linkURL: "http://www.skanderborg-ungdomsskole.dk/"
        },
		{
            img: "ungepuljen.jpg",
            desc: "Webdesign, flash, jQuery, css, html, cms and user frontend administration",
            linkText: "Ungepuljen",
            linkURL: "http://www.ungepuljen.dk/"
        },
        {
            img: "designteknik.jpg",
            desc: "Webdesign, portable support for mobile & tablet, jQuery, css, html",
            linkText: "Design og Teknik",
            linkURL: "http://www.designteknik.dk/"
        }, 
		{
            img: "wicotech.jpg",
            desc: "Webdesign, jQuery, css, html5",
            linkText: "Wicotech",
            linkURL: "http://www.wicotech.com/"
        },
		{
            img: "haarfein.jpg",
            desc: "Webdesign, flash banner, css, html, Adobe inContext Editing CMS",
            linkText: "haarfein",
            linkURL: "http://www.haarfein.dk/"
        },
        {
            img: "classic-mustang.jpg",
            desc: "Webdesign, webshop, flash integration, jQuery, css, html, CMS and user frontend administration",
            linkText: "Classic Mustang",
            linkURL: "http://www.classic-mustang.dk/"
        },
        {
            img: "marienlund-klinikken.jpg",
            desc: "Webdesign, video integration, jQuery, css, html, cms and user frontend administration",
            linkText: "Marienlund Klinikken",
            linkURL: "http://www.marienlund-klinikken.dk/"
        },
        {
            img: "richmont.jpg",
            desc: "Webdesign, portable support for mobile, css, html",
            linkText: "Richmont General Insurance Services",
            linkURL: "http://www.richmontinsurance.com/"
        }
    ];
    
    var loginBoxIsShown;
    function positionLoginBox() {

        var topo = parseInt($("#tbdLnkCmsLogin").offset().top + 40);
        var lefto = parseInt($("#tbdLnkCmsLogin").offset().left);
        var distance2right = parseInt($(window).width()) - lefto;
        var newLefto = lefto + distance2right;
        if (debug) {
            console.log("Top offset: " + topo + " | Left offset: " + lefto + " | Distance to right side: " + distance2right);
        }
        $("#tbdLoginBox").offset({ top: topo, left: newLefto });
        $("#tbdLoginBox").hide();
        loginBoxIsShown = false;
    }

    $(function () {

        for (var oW = 0; oW < recentWork.length; oW++) {
            var ourWorkImg = document.createElement("IMG");
            ourWorkImg.setAttribute("id", "oWImg_"+(oW+1));
            ourWorkImg.setAttribute("src", "gfx/showcase/" + recentWork[oW].img);
            ourWorkImg.setAttribute("alt", recentWork[oW].linkText);
            ourWorkImg.setAttribute("width", "685");
            ourWorkImg.setAttribute("height", "459");
            $("#ourWorkCycle").append(ourWorkImg);
        }
        /* Activate Cufon Fonts */
        Cufon.replace("#showcase h2, #expertises h2, #movies h2, #about h2, #contact h2, #applications h2, #location h2, #mobile h2", {
            fontFamily: 'stampete',
            textShadow: '1px 1px #000'
        });
        Cufon.replace("#showcase .showcase-ekstra, #expertises .showcase-ekstra, #movies .showcase-ekstra, #about .showcase-ekstra, #contact .showcase-ekstra, #applications .showcase-ekstra, #mobile .mobile-ekstra, .htabs h2", {
            fontFamily: 'dearjoe3',
            textShadow: '1px 1px #000'
        });

        //Activate local scrolling
        $.localScroll();
        
        /* Activate fold plugin */
        $('#target').fold();

        /* Activate the showcase cycle */
        $("#ourWorkCycle").cycle({
            before: function (currSlideElement, nextSlideElement, options, forwardFlag) {
                var imgID = Number($(nextSlideElement).attr("id").substr(6));
                var recentWorkIdx = imgID - 1;
                $("#ourWorkCycle_txt p:first-child").html(recentWork[recentWorkIdx].desc);
                $("#ourWorkCycle_link").attr("href", recentWork[recentWorkIdx].linkURL);
                $("#ourWorkCycle_link").html("visit " + recentWork[recentWorkIdx].linkText);
            }
        });
        /* Event handlers to manually control the prev/next buttons */
        $(".showcase-next").click(function (e) {
            e.preventDefault();
            $("#ourWorkCycle").cycle('pause');
            $("#ourWorkCycle").cycle('next');
        });
        $(".showcase-prev").click(function (e) {
            e.preventDefault();
            $("#ourWorkCycle").cycle('pause');
            $("#ourWorkCycle").cycle('prev');
        });

        /* Login box event handlers & positioning */
        $("#tbdLnkCmsLogin").bind("click", function (e) {
            e.preventDefault();
            $("#tbdLoginBox").show();
            var btnCmsLoginX = parseInt($(this).position().left) -50;
            if (debug) {
                console.log("Button left: " + btnCmsLoginX);
            }
            $("#tbdLoginBox").animate(
                { "left": btnCmsLoginX },
                { "duration": "slow", "easing": "easeOutBack" }
                );
            loginBoxIsShown = true;
        });
        $("#tbdLnkCloseCmsLogin").bind("click", function (e) {
            e.preventDefault();
            var newLefto = parseInt($(window).width());
            if (debug) { console.log("New left: " + newLefto) }
            $("#tbdLoginBox").animate(
                { "left": newLefto },
                { "duration": "slow",
                  "easing": "easeInBack",
                  "complete": function() { positionLoginBox(); } 
                }
                );
            loginBoxIsShown = false;
        });

        $(window).resize(function () {
            if (!loginBoxIsShown) { positionLoginBox(); }
        });

        positionLoginBox();

        /* Tabbed area functionality */
        //if this is not the first tab, hide it
        jQuery(".tab:not(:first)").hide();

        //to fix u know who
        jQuery(".tab:first").show();

        //when we click one of the tabs
        jQuery(".htabs a").click(function () {
            //get the ID of the element we need to show
            stringref = jQuery(this).attr("href").split('#')[1];
            //hide the tabs that doesn't match the ID
            jQuery('.tab:not(#' + stringref + ')').hide();
            //fix
            if (jQuery.browser.msie && jQuery.browser.version.substr(0, 3) == "6.0") {
                jQuery('.tab#' + stringref).show();
            }
            else
            //display our tab fading it in
                jQuery('.tab#' + stringref).fadeIn();
            //stay with me
            return false;
        });

        //Larger thumbnail preview 

        $("span.qr-code").hover(function() {
            $(this).css({'z-index' : '10'});
            $(this).find('img').addClass("hover").stop()
                   .animate({
			marginBottom: '0px', 
			marginRight: '0px',  
			bottom: '0%', 
			right: '0%', 
			width: '230px', 
			height: '230px',
			padding: '20px' 
                    }, 200);
	
        } , function() {
            $(this).css({'z-index' : '0'});
            $(this).find('img').removeClass("hover").stop()
                   .animate({
			marginBottom: '0', 
			marginRight: '0',
			bottom: '15px', 
			right: '15px', 
			width: '50px', 
			height: '50px', 
			padding: '0px'
                    }, 400);
        });
        
        $("#socialLink").fancybox({
            padding: '0px',
            margin: '0px',
            transitionIn: 'elastic',
            transitionOut: 'elastic',
            onstart: (function(d, s, id) {
                var js, fjs = d.getElementsByTagName(s)[0];
                if (d.getElementById(id)) {return;}
                js = d.createElement(s); js.id = id;
                js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
                fjs.parentNode.insertBefore(js, fjs);
            }(document, 'script', 'facebook-jssdk'))
        });
    });

})(jQuery, window, document);

