////////////////////////////////////////////////////////////////////////////////
// Bangras Site Code - Requires JQuery
////////////////////////////////////////////////////////////////////////////////

$(document).ready(function() {
    //-----------------------------------------------------------
    // Variable definitions
    //-----------------------------------------------------------
    var _width = 0;
    var _height = 0;
    var _scrollX = 0;
    var _scrollY = 0;
    var _popupIsScaling = false;
    
    //-----------------------------------------------------------
    // Scroll to top of page
    //-----------------------------------------------------------
    $('a.back_to_top').click(function(){
        $.scrollTo(0, 400);
        return false;
    });
    
    //-----------------------------------------------------------
    // Scroll to the Bangras story content
    //-----------------------------------------------------------
    $('a#bangras_story').click(function(){
        var pathName = window.location.href;
        if ((pathName.indexOf('?p=') > -1) || (pathName.indexOf('?m=') > -1)) {
            return true;
        } else {
            $.scrollTo($('div#story'), 400);
        }
        return false;
    });
    
    //-----------------------------------------------------------
    // Scroll to flavour descriptions
    //-----------------------------------------------------------
    $('a#explore').click(function(){
        var pathName = window.location.href;
        if ((pathName.indexOf('?p=') > -1) || (pathName.indexOf('?m=') > -1)) {
            return true;
        } else {
            $.scrollTo($('div#flavours'), 400);
        }
        return false;
    });
    
    //-----------------------------------------------------------
    // Scroll to The Other Mr Singhs
    //-----------------------------------------------------------
    $('a#other-mr-singhs').click(function(){
        var pathName = window.location.href;
        if ((pathName.indexOf('?p=') > -1) || (pathName.indexOf('?m=') > -1)) {
            return true;
        } else {
            $.scrollTo($('div#other-mr-singhs'), 400);
        }
        return false;
    });
    
  //-----------------------------------------------------------
    // Scroll to Where to Buy
    //-----------------------------------------------------------
    $('a#where-to-buy').click(function(){
        var pathName = window.location.href;
        if ((pathName.indexOf('?p=') > -1) || (pathName.indexOf('?m=') > -1)) {
            return true;
        } else {
            $.scrollTo($('div#where-to-buy'), 400);
        }
        return false;
    });
    
    //-----------------------------------------------------------
    // View film button has been clicked
    //-----------------------------------------------------------
    $('a#film').click(function(e) {
        _width = 534;
        _height = 321;
        _targetURL = $('a#film').attr('href');
        $('#popup_content').removeClass("popup_game");
        $('#popup_content').addClass("popup_film");
        $('#overlay').show();
        var position = $('a#film').position();
        $('#popup').css('left', e.pageX + 'px');
        $('#popup').css('top', e.pageY + 'px');
        $('#popup').css('width', '1px');
        $('#popup').css('height', '1px');
        enlargeBox();
        return false;
    });
    
    //-----------------------------------------------------------
    // Play game button has been clicked
    //-----------------------------------------------------------
    $('a#game').click(function(e) {
        _width = 655;
        _height = 490;
        _targetURL = $('a#game').attr('href');
        $('#popup_content').removeClass("popup_film");
        $('#popup_content').addClass("popup_game");
        $('#overlay').show();
        var position = $('a#game').position();
        $('#popup').css('left', e.pageX + 'px');
        $('#popup').css('top', e.pageY + 'px');
        $('#popup').css('width', '1px');
        $('#popup').css('height', '1px');
        enlargeBox();
        return false;
    });
    
    //-----------------------------------------------------------
    // Close panel button has been clicked
    //-----------------------------------------------------------
    $('a#close_button').click(function(e) {
        closePopup();
        return false;
    });
    
    //-----------------------------------------------------------
    // Overlay has been clicked
    //-----------------------------------------------------------
    $('#overlay').click(function(e) {
        if (!_popupIsScaling) {
            closePopup();
        }
        return false;
    });
    
    //-----------------------------------------------------------
    // Close popup
    //-----------------------------------------------------------
    function closePopup() {
        $('#inner_content').remove();
        $('#popup_content').html("");
        $('#popup').hide();
        $('#overlay').hide();
    }
    
    //-----------------------------------------------------------
    // Animate popup coming into view
    //-----------------------------------------------------------
    function enlargeBox() {
        _popupIsScaling = true;
        var clientWidth = document.documentElement.offsetWidth;
        var clientHeight = $(window).height();
        getScrollXY();
        var targetX = ((clientWidth / 2) - (_width/2)) + document.documentElement.scrollLeft + 'px';
        var targetY = ((clientHeight / 2) - (_height/2)) + _scrollY + 'px';
        $('#popup').animate({left: targetX, top: targetY, width: _width, height: _height}, 'medium', loadContent);
    }
    
    //-----------------------------------------------------------
    // Load content into popup
    //-----------------------------------------------------------
    function loadContent() {
        $('#popup_content').append("<div id='inner_content'></div>");
        $('#inner_content').load(_targetURL, "", fadeContentIn);
    }
    
    //-----------------------------------------------------------
    // Fade in loaded content
    //-----------------------------------------------------------
    function fadeContentIn() {
        _popupIsScaling = false;
        $('#inner_content').hide();
        $('#inner_content').fadeIn("fast");
    }
    
    //-----------------------------------------------------------
    // Get scroll offsets
    //-----------------------------------------------------------
    function getScrollXY() {
        if( typeof( window.pageYOffset ) == 'number' ) {
            //Netscape compliant
            _scrollY = window.pageYOffset;
            _scrollX = window.pageXOffset;
        } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
            //DOM compliant
            _scrollY = document.body.scrollTop;
            _scrollX = document.body.scrollLeft;
        } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
            //IE6 standards compliant mode
            _scrollY = document.documentElement.scrollTop;
            _scrollX = document.documentElement.scrollLeft;
        }
      }
});