﻿/* all sorts of odds and ends for bandai uk */

/* selfPopup
 * open popup window with href determined from the href attribute on the anchor:
 * <a href="index.html" onclick="return selfPopup(this,300,400);">Index</a>
 * dominic winsor, get2dom.com
 */

/* I take no responsibility for selfPopup. Tweaks, enhancements and new features added by :Dan Wellman 2010 */

function selfPopup( loc, x, y ) { // <-- urgh
   LeftPosition = (screen.width) ? (screen.width-x)/2 : 0;
   TopPosition = (screen.height) ? (screen.height-y)/2 : 0;
   window.open(loc.href,"","toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=1,copyhistory=0,width="+x+",height="+y+',left='  +LeftPosition +  ',top='  +TopPosition +"");
   return false;
}

// stuff to brand Google Custom Search box
function clearSearchBg() {
    csbeBg = "#fff url(http://www.google.com/coop/intl/en/images/google_custom_search_watermark.gif) no-repeat";
    if ($(".search-box").val() == '') { $(".search-box").css("background", csbeBg); }
}

//add latest tweet
var twitterCallback = function (data) {

    //linkify
    var text = data[0].text,
        linkregex = /((http\:\/\/|https\:\/\/|ftp\:\/\/)|(www\.))+(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/gi,
        atregex = /\@([a-z0-9_\-]+)/gi;

    text = text.replace(linkregex, function (value) {
        return '<a href="' + value + '">' + value + '</a>';
    });

    text = text.replace(atregex, function (value) {
        return '<a href="http://twitter.com/' + value.split('@')[1] + '">' + value + '</a>';
    });

    //insert
    jQuery("#tweet").html(text);

    //add timestamp
    var tStamp = data[0].created_at,
        dateArray = tStamp.split(" ");
        tweetLink = $("<a></a>", {
            text: dateArray[1] + " " + dateArray[2] + " " + dateArray[5],
            href: "http://twitter.com/" + data[0].user.screen_name + "/status/" + data[0].id,
            "class": "tweet-link"
        }).appendTo("#tweet");
}

$(document).ready(function () {

    //show cufon elements
    $(".cufon-replace").css("visibility", "visible");

    //detect Mac to fix a position issue with hero panel
    var ua = navigator.userAgent,
        body = $("body");

    body.addClass((ua.match(/Mac/i)) ? "mac" : "");

    //detect webkit to fix a rendering issue with hero panel
    body.addClass((ua.match(/AppleWebKit/i)) ? "webkit" : "");

    $(".search-box").focus(function () {
        $(this).css("background", "#fff");
    }).blur(function () {
        clearSearchBg();
    });

    //add class 'last' to last child in top-five list
    if ($("#top-five").length > 0) {
        $("#top-five").find("ul").children(":last").addClass("last")
    }

    //add script element for twitter
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src = "http://twitter.com/statuses/user_timeline/BandaiToysUK.json?callback=twitterCallback&amp;count=1"

    $("body").append(script);

    //hero panel
    var hero = document.getElementById("hero"),
		event = (ua.match(/iPad/i) || ua.match(/iPhone/i)) ? "touchstart" : "click";

    $(".hero-button", hero).bind(event, function (e) {
        e.preventDefault();

        var length = $(this).prevAll().length,
            animLength = length * 130,
            panelID = $(this).attr("id").split("-")[0];

        $(".featured", hero).removeClass("featured");
        $(".pre-featured", hero).removeClass("pre-featured");

        $(this).parent().children(":first").animate({
            marginTop: -animLength
        }, "slow", function () {
            $(this).css("marginTop", 0);

            //added for xmas 2011
            if (!$(".indicator", hero).is(":visible")) {
                $(".indicator", hero).show();
            }
            if ($("#thundercats-button").hasClass("initial-featured")) {
                $("#thundercats-button").removeClass("initial-featured")
            }

            $("#heroPanels").children().not(".indicator").each(function () {
                ($(this).is(":visible")) ? $(this).css("display", "none") : null;
            });
            $("#" + panelID).show();

            for (var x = 0; x < length; x++) {
                $(".right-col", hero).children(":first").appendTo(".right-col");
            }

            $(".featured").next().addClass("pre-featured");
        });

        $(this).addClass("featured").css("marginTop", "").next().addClass("pre-featured").css("marginTop", "");
    });
});
