
var menuHistory = new Array();
var currentPage = null;
var globalPrevTab = null;

var forcedTab = null;

$(window).konami(function () {

    window.location = "http://www.youtube.com/watch?v=EK2tWVj6lXw&ob=av3e";

});


$(window).hashchange(function () {
  
  if (location.hash != '#'){
      var page = location.hash.split('#');
      if (page[1] != null) {
        pageAndTab = page[1].split('_');
        
        forcedTab = pageAndTab[1];
        
   
          currentPage = pageAndTab[0];
          
          
          loadNewPage(currentPage);
          globalPrevTab = null;
          pageTracker._trackPageview("/pages/"+currentPage+".aspx");
      }
    }

});


$(document).ready(function () {



    $('.youtubeplayer').live('click', function () {

        var link = $(this).attr('rel');
        $('#overlay').fadeIn(300, function () {
            $('#video-player').show();
            $('#video-player').animate({
                marginTop: "-271px"
            }, { duration: 1000, easing: "easeOutElastic", complete: function () {
                $('#video-player iframe').attr('src', link);
            } 
            });

        });

        return false;

    });

    $('#overlay').live('click', function () {

        $('#video-player').animate({
            marginTop: "-1200px"
        }, { duration: 300, easing: "easeInBack", complete: function () {
            $('#overlay').fadeOut(300);
            $('#video-player iframe').attr('src', '');
            $('#video-player').hide();
        } 
        });

    });

    if ($.browser.msie && $.browser.version == "6.0") {
        window.location = "/hold.html";
    }

    if (window.location.hash) {

        pageAndTab = window.location.hash.substr(1).split('_');

        forcedTab = pageAndTab[1];
        loadNewPage(pageAndTab[0]);

    } else {
        loadNewPage('home');
    }

    updateTwitterStatus();


    // Hover Menu Magic
    $('li').hover(

    function () {
        $('img', this).fadeIn(100).css('display', 'block');


    }, function () {
        if ($(this).hasClass('selected') == false) {
            $('img', this).fadeOut(500);
            //$('.selected img').fadeIn(); 
        }
    });

    // Click Menu Magic    
    $('#header #nav li a').bind('click', function () {

        /*
        // Update menu
        $('.selected img').hide();
        $('.selected').animate({
        lineHeight: 25
        });
        $('.selected').removeClass('selected');

        $(this).parent().animate({
        lineHeight: 100
        }, 300);

        $(this).parent().addClass('selected');
        */

        // Get raw link filename
        var link = $(this).attr('href').split("/");
        link = link[1].split(".");

        window.location = "#" + link[0];

        // Disable click
        return false;
    });

});


function setMenuCategory(cat){
  

  currently = $('#nav .selected a').attr('id');
    
  if('m_'+cat != currently){

    $('#nav .selected img').hide();
    $('#nav .selected').animate({
        lineHeight: 25
    });
    
    $('#nav .selected').removeClass('selected');
    
    $('#m_'+cat).parent().animate({
        lineHeight: 100
    }, 300);
          
       $('#m_'+cat).parent().addClass('selected');
       $('#m_'+cat+' img').fadeIn(100).css('display', 'block');

  }
  
}

function workEyeCandy() {

    var menuIcons = $('body').find('.work_thumb_container');
    var i = 0;


    menuFadeThrough = setInterval(function () {


        if (i == menuIcons.length) {
            clearInterval(menuFadeThrough);
        }

        i++;

    }, 100);

}

function loadNewPage(page) {
    var cleanPage = page.replace("/", "").replace("/","")

    $('#loading').show();
   
    var oldPageHeight = $("div[rel*='view']").height();
 
    // Fadeout old div
    $("div[rel*='view']").animate({
        opacity: 0,
        height: oldPageHeight

    }, 200, function () {

        // Once fade out complete, remove the div holding it
        $(this).remove();

        // Create a new div to load content in
        $('#load').append('<div id="loaded_' + cleanPage + '" rel="view"></div>');

        // Give it the height of the original content
        $('#load').css('height', oldPageHeight);

        $('#loaded_' + cleanPage).css('display', 'none');

        // Load content into newly created driv
        $('#loaded_' + cleanPage).load('pages/' +page + '.aspx', function () {


            // Retain the loaded divs height
            var pageHeight = $('#loaded_' + cleanPage).height();

            // Set it to hold height so we can animate it sliding down
            $('#loaded_' + cleanPage).css('height', oldPageHeight);
            $('#loaded_' + cleanPage).css('display', 'none');

            // Slide it down
            $('#load').animate({
                height: pageHeight
            }, 500, function () {

                $('#loaded_' + cleanPage).css('height', pageHeight);
                $('#loaded_' + cleanPage).fadeIn(500, function () {

                    if (!window.XMLHttpRequest) {
                        $('#loaded_' + cleanPage).supersleight();
                    }

                });

                // All Done
                $('#loading').fadeOut(50);

            });

        });

    });

}

function setPageTheme(stylesheet) {

    if ($('#theme').attr('href') == stylesheet) {} else {
        $('#flash').fadeIn(100, function () {
            $("#theme").attr("href", stylesheet);
            $('#flash').fadeOut(1000);

        });
    }

}

function forceTab(tab) {

  pageTracker._trackPageview("/pages/"+tab+".aspx");
  
    $('#tabs .selected').removeClass();

    $('.tabView[rel|=' + globalPrevTab + ']').fadeOut(200, function () {

        var newTabHeight = $('.tabView[rel|=' + tab + ']').height();

        $('#load').animate({
            height: newTabHeight
        }, 300, function () {

            $('#loaded_' + currentPage).height(newTabHeight);

            $('.tabView[rel|=' + tab + ']').fadeIn(300);

            globalPrevTab = tab;

        });

    });

    return false;

}

function playYouTube(){
  
  $('#overlay').show();
  video = $(this).attr('rel');
    
  $('.youtube-player').attr('src',video);
    
  return false;

}

function updateTwitterStatus() {

    $.getJSON('/jSon/TwitterConnectorWeb.ashx', function (data) {
    
        $('#twitterstatus').html(data.twText);
    });

}




function bind() {
  
  if($.browser.webkit){
    $('#play-button').append('<div id="pulse"></div>');
  }
  
    var homeviews = $('#scroller #views').find('li');
    
    $(homeviews).hide();
  //change it to two  
    randomView = Math.floor(Math.random()*4);
    
    
    $(homeviews[randomView]).show();
   
    $('#tags').find('a').bind('click', function () {

        $('#tags .selected').removeClass('selected');
        $(this).addClass('selected');

        tag = $(this).attr('alt');

        if (tag == "all") {
            $('.work_thumb_container').show('fast');
        } else {

            thumbs = $('.work_thumb_container');
            i = 0;

            while (i < thumbs.length) {
                if ($(thumbs[i]).attr('rel') == tag) {
                    $(thumbs[i]).show('fast');
                } else {
                    $(thumbs[i]).hide('fast');
                }
                i++;
            }

        }

        return false;
    });

    $('.work_thumb_container').hover(function () {
        $(this).children('.overlay').fadeIn(150);
    }, function () {
        $(this).children('.overlay').fadeOut(500);
    });

    $('#button_back').hover(

    function () {
        $('#button_back').animate({
            marginLeft: '-=10px'
        }, 300);
    }, function () {
        $('#button_back').animate({
            marginLeft: '+=10px'
        }, 300);
    });


    $('.thumbnail').bind('click', function () {
    
            imagePath = $(this).children().attr('alt');          
        
          $('.photo_container').css('background-image', 'url("' + imagePath + '")');
        
      
        return false;

    });


    $('.toolTip').hover(

    function () {
        var tip = $(this).attr('alt');
        $(this).prepend('<div id="toolTipPop">' + tip + '</div>');
        $(this).css('text-shadow', '#8ACE19 0px 0px 5px');
    }, function () {

        $('.toolTip').css('text-shadow', '0px 0px 0px');

        $('#toolTipPop').fadeOut('slow', function () {
            $('#toolTipPop').remove();
        });

    });

    // Setup tabs
    var tabsArray = $('.tabView');
    // Hide all tabs
    $('.tabView').hide();

  
  if(forcedTab == null){
      // Show first tab only
      $(tabsArray[0]).show();
      globalPrevTab = $(tabsArray[0]).attr('rel');
    } else {
      $('.tabView[rel|='+forcedTab+']').show();
      $('#tabs .selected').removeClass();
      $('#tabs li a[rel|='+forcedTab+']').parent().addClass('selected');
      globalPrevTab = forcedTab;
      forcedTab = null;
    }

    $('#tabs').find('a').bind('click', function () {

        var newTab = $(this).attr('rel');
        
        pageTracker._trackPageview("/pages/"+newTab+".aspx");

        $('#tabs .selected').removeClass();
        $(this).parent().addClass('selected');

        // Fade out old tab content
        $('.tabView[rel|=' + globalPrevTab + ']').fadeOut(200, function () {
      
            var newTabHeight = $('.tabView[rel|=' + newTab + ']').height();

            $('#load').animate({
                height: newTabHeight
            }, 300, function () {

                $('#loaded_' + currentPage).height(newTabHeight);

                $('.tabView[rel|=' + newTab + ']').fadeIn(300);

                globalPrevTab = newTab;

            });

        });

        return false;

    });



}

// Redefine animation
jQuery.easing['jswing'] = jQuery.easing['swing'];

jQuery.extend( jQuery.easing,
{
  def: 'easeOutQuad',
  swing: function (x, t, b, c, d) {
    //alert(jQuery.easing.default);
    return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
  },
  easeOutQuad: function (x, t, b, c, d) {
    return -c *(t/=d)*(t-2) + b;
  },
  easeOutElastic: function (x, t, b, c, d) {
    var s=1.70158;var p=0;var a=c;
    if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
    if (a < Math.abs(c)) { a=c; var s=p/4; }
    else var s = p/(2*Math.PI) * Math.asin (c/a);
    return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
  },
  easeOutBounce: function (x, t, b, c, d) {
    if ((t/=d) < (1/2.75)) {
      return c*(7.5625*t*t) + b;
    } else if (t < (2/2.75)) {
      return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
    } else if (t < (2.5/2.75)) {
      return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
    } else {
      return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
    }
  },easeInBack: function (x, t, b, c, d, s) {
      if (s == undefined) s = 1.70158;
      return c*(t/=d)*t*((s+1)*t - s) + b;
  }
});

