/*global $ */

$(document).ready(function(){
  insertFlashMovie();
})

function activate(){
  $("#content, #banner").css({"display":"none"});
  if(location.pathname == "/"){
    $("a").not($(".popup")).not($(".autobriefing")).not("a[class*='external']").address();
    $.address.change(function(event) {
      var area = event.value.substring(1,event.value.length) || "welcome";
      var flashArea = area.toTitleCase().replace(/-/g, "").replace(area.substring(0,1).toUpperCase(),area.substring(0,1));
      //console.log(flashArea);
      $.address.title(area.replace(/-/g," ").toTitleCase() + " - Yachting Financial Solutions");
      getFlashMovie("background").goTo(flashArea);
      if($("#content").css("display") != "none") $("#content").fadeOut();
      if($("#banner").css("display") != "none") $("#banner").fadeOut();
      $("#menu a").removeAttr("class");
      if(event.value != "/") $("#menu a[href*="+ event.value +"]").addClass("active");
    });
  }else{
    window.location = $.address.value() == "/" ? ("/#" + location.pathname) : ("/#" + $.address.value());
  }
}

function getFlashMovie(movieName) {
  var isIE = navigator.appName.indexOf("Microsoft") != -1;
  return (isIE) ? window[movieName] : document[movieName];
}

function insertFlashMovie(){
  $("#wrapper").before("<div id='background'></div>");
  
  var flashvars = { startAt : getCurrent().area };
  var params = {
    bgcolor : "transparent",//"#283E16",
    quality : "high",
    wmode   : "transparent",
    menu    : "false",
    base    : "/resources",
    allowScriptAccess : "always"
  };
  var attributes = {};
  swfobject.embedSWF("/resources/swfs/main.swf", "background", "0", "0", "9.0.0", false, flashvars, params, attributes);
}

function getCurrent(){
  var array = window.location.toString().split("/");
  var last = array[array.length-1];
  if(array.length > 2 && last != ""){
    var currentPath = last.toTitleCase().replace(/-/g,"_");
    var currentArea = last.toTitleCase().replace(/-/g,"");
    currentArea = currentArea.replace(currentArea.substring(0,1),currentArea.substring(0,1).toLowerCase());
  }else{
    currentArea = "index";
    currentPath = "index";
  }
  
  return {area:currentArea,path:currentPath.toLowerCase()};
}

function insertPage(page){
  
  if (page == 'partners') {
    $('#vcard').html('<p><strong>YFSOL Spain</strong></p><p>t. +34 971 727 198</p><p>e. <a href="mailto:spain@yfsol.com">spain@yfsol.com</a></p><p>s. <a href="skype://yfs.spain">skype me</a></p>');
  } else {
    $('#vcard').html("<p><strong>Galerie du Port</strong> . Rue Lacan</p><p>Antibes . France <a href=\"http://maps.google.co.uk/maps?source=s_q&amp;f=q&amp;hl=en&amp;geocode=&amp;q=26+Rue+Lacan+06600+Antibes&amp;sll=53.800651,-4.064941&amp;sspn=17.497481,45.834961&amp;ie=UTF8&amp;hq=&amp;hnear=26+Rue+Lacan,+06600+Antibes,+Maritime+Alps,+Provence-Alpes-C%C3%B4te+d'Azur,+France&amp;ll=59.712097,9.140625&amp;spn=66.192425,149.414063&amp;z=2&amp;iwloc=A&amp;output=embed\" class=\"popup\">[see map]</a></p><p>t. +33 04 9334-3600</p><p>e. <a href=\"mailto:info@yfsol.com\">info@yfsol.com</a></p><p>s. <a href=\"skype://yfsol.france\">skype me</a></p>");
    $(".popup").fancybox({type : "iframe"});
  }
  
  $("#content").html("");
  
  $.get(getCurrent().path, function(data){
    $("#content").html(data).fadeIn();
    
    var banner = page == "#" || page == "welcome" || page == "index" ? "/resources/images/banners/home.jpg" : "/resources/images/banners/" + page + ".jpg";
    
    if(page == '#' || page == 'welcome' || page == 'index' || page == 'about' || page =='plan' || page == 'hub' || page == 'voyager' || page == 'files' || page == 'partners'){
      $("#banner").css({"background-image":"url('"+banner+"')"}).fadeIn();
    }else{
      $("#banner").fadeOut();
    }
    
    if (page === 'hub') {
        console.log('crazy thing 2');
        $('#autobriefing_archive').html(document.getElementById('autobriefing_archive').innerHTML);
         $(".autobriefing").fancybox({type : "iframe", width: 750, height: 500});
        loadTweets();
    }
    
  });
}

function trace(msg){
  console.log(msg);
}

function popUp(URL) {
    var day = new Date();
    var id = day.getTime();
    var width = 1024;
    var height = 650;
    var top = (screen.availHeight/2) - (height/2);
    var left = (screen.availWidth/2) - (width/2);
    eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width="+ width +",height="+ height +",left="+ left +",top="+ top +"')");
}

function get(item){
    var obj = {};
    var array = [];
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');

    for(var i = 0; i < hashes.length; i++)
        {
            array = hashes[i].split('=');
            obj[array[0]] = array[1];
        }
    
        return obj[item];
}

String.prototype.toTitleCase = function() {
    return this.replace(/([\w&`'‘’"“.@:\/\{\(\[<>_]+-? *)/g, function(match, p1, index, title) {
        if (index > 0 && title.charAt(index - 2) !== ":" &&
        	match.search(/^(a(nd?|s|t)?|b(ut|y)|en|for|i[fn]|o[fnr]|t(he|o)|vs?\.?|via)[ \-]/i) > -1)
            return match.toLowerCase();
        if (title.substring(index - 1, index + 1).search(/['"_{(\[]/) > -1)
            return match.charAt(0) + match.charAt(1).toUpperCase() + match.substr(2);
        if (match.substr(1).search(/[A-Z]+|&|[\w]+[._][\w]+/) > -1 || 
        	title.substring(index - 1, index + 1).search(/[\])}]/) > -1)
            return match;
        return match.charAt(0).toUpperCase() + match.substr(1);
    });
};

String.prototype.empty = function(){
  return this.replace(/\s/g,"") == "";
};

