var loaded = false;


jQuery.fn.responsive_nav = function(active_class, turning_point){
  var element = jQuery(this);
  
  element.before("<select id=\"mobile_navigation\"><optgroup label=\"Navigate\"></optgroup></select>");
  
  element.children("li").each(function(){
    var selected = "";

    if(jQuery(this).hasClass(active_class)){ selected = "selected=selected"; }

    jQuery("#mobile_navigation optgroup").append("<option " + selected + " value=\"" + jQuery(this).children("a").attr("href") + "\">" + jQuery(this).text() + "</option>");
  });

  jQuery("#mobile_navigation").hide().change(function(){
    document.location = jQuery(this).val();
  });
  
  var switch_to_dropdown = function(){
    if(jQuery(window).width() < turning_point){
      jQuery("#mobile_navigation").show();
      element.hide();      
    }else{
      jQuery("#mobile_navigation").hide();
      element.show();
    }
  }
  
  switch_to_dropdown();
  
  jQuery(window).resize(function(){
    switch_to_dropdown();
	 
  });
}

var init_state = function(){  

  jQuery("#blog article").height("auto");
  
  if(jQuery(window).width() >= 760){
    if(jQuery("#two_pane").size() === 0){ jQuery("head").append("<link href=\"" + template_url + "/css/760.css\" media=\"screen\" rel=\"stylesheet\" type=\"text/css\" id=\"two_pane\" />"); }
  }else{
    jQuery("head #two_pane").remove();
  }
  
  if(jQuery(window).width() >= 1110 && jQuery(window).height() > 600){
    
    jQuery("#work h3 a").click(function(e){ e.preventDefault(); });
    
    if(jQuery("#horizontal_scrolling").size() === 0){ jQuery("head").append("<link href=\"" + template_url + "/css/1100.css\" media=\"screen\" rel=\"stylesheet\" type=\"text/css\" id=\"horizontal_scrolling\" />"); }
  
    jQuery("#horizontal_scrolling").ready(function(){
      setTimeout(function(){
        var top_offset = (jQuery(window).height() - jQuery("#main_head .vcentre").height()) / 2 - 10;
        
        if(jQuery(window).height() >= 680){
          jQuery("#main_head .vcentre").css("margin-top", top_offset);
          jQuery("#work").css("margin-top", top_offset + 70);
        }else{
          jQuery("#work").css("margin-top", 132);
        }
        
        if(jQuery("#blog").size() > 0){
          var height = 0;

          jQuery("#blog article").each(function(){ if(jQuery(this).height() > height){ height = jQuery(this).height(); } });

          jQuery("#blog article").height(height);
        }
      }, 100);
      
      setTimeout(function(){ if(window.location.hash){ jQuery("a[href=#" + window.location.hash.split(":")[1] + "]").click(); } }, 900);
    });
  }else{
    jQuery("head #horizontal_scrolling").remove();
    jQuery("#main_head .vcentre").css("margin-top", 60);
    
    var top_margin = 0;
    
    if(jQuery(window).width() > 760){ top_margin = 180; }
    
    jQuery("#work").css({ 
      "margin-left": 0,
      "margin-top": top_margin
    });
  }
  
  if(!loaded){
    jQuery("#main_head").css("margin-left", -450).animate({ "margin-left": 0 }, 240, function(){
      jQuery(".main, #progress, #next, #social_nav, #main_nav, #contacts, #main_head .vcentre").fadeTo(600, 1);
    });
    
    jQuery(".main, #progress, #next, #social_nav, #main_nav, #contacts, #main_head .vcentre").fadeTo(0, 0);
  }
  
  loaded = true;
  
}
 init_state();

jQuery(document).ready(function($){ 
 jQuery("#main_head").css("margin-left", -450).animate({ "margin-left": 0 }, 240, function(){
      jQuery(".main, #progress, #next, #social_nav, #main_nav, #contacts, #main_head .vcentre").fadeTo(600, 1);
	  });
	  jQuery(".main, #progress, #next, #social_nav, #main_nav, #contacts, #main_head .vcentre").fadeTo(0, 0);

  var click_history = [];
  
  if('ontouchstart' in document.documentElement){
    $("body").addClass("touchable");
  }else{
    $("body").addClass("untouchable");
  }
  
  $("#main_nav ul").responsive_nav("current_page_item", 760);
  
  $('.button').prepend('<span>' + '+' + '</span>');
  
  $(window).resize(function(){ init_state(); });
  
  
  $("#work article:first-child").addClass("active");
  
  $("body.home, body.single-project").append("<ul id=\"progress\"></ul><div id=\"next\"><a href=\"#\">&gt;</a></div>");
  
  var project_count = 0;
  
  $("#work article").each(function(){
    project_count++;
    
    var appended_class = "";
    
    if(project_count === 1){ appended_class = " class=\"active\""; }
    
    $("#progress").append("<li" + appended_class + "><a href=\"#"+ $(this).attr("id") +"\">" + project_count + "</a></li>");
  });
  
  $("#progress li a").css("width", (540 / project_count) - 2);
  
  $("#next a").click(function(e){
    e.preventDefault();
	
    
    if($("#next a").text() === ">"){  
      $("#progress .active").next().children("a").click();
    }else{
      $("#progress li:first-child a").click();
    }
  });
  
  $(document).keydown(function(e){
    if($("#horizontal_scrolling").size() > 0){
      if(e.keyCode === 37){
        e.preventDefault();
    
        $("#progress .active").prev().children("a").click();
      }else if(e.keyCode === 39){
        e.preventDefault();
    
        $("#progress .active").next().children("a").click();
      }else if(e.keyCode === 13 && $("body").hasClass("home")){
        if($("#work .active").hasClass("project")){ document.location = $("#work .active .button").attr("href"); }
      }
    }
  });
  
  $("#work article").click(function(){
    if($(window).width() > 1100){
      if($("#progress li.active a[href=#" + $(this).attr("id") + "]").size() === 0){ $("#progress li a[href=#" + $(this).attr("id") + "]").click(); }
    }
  });
  
  var back_button_clicked = false;
  var click_history = [];
  
  $("#progress a").click(function(e){
    e.preventDefault();
    
    var left_offset = 0;
    
    
    $("#progress li").removeClass("active");
    $(this).parents("li").addClass("active");
    
    $(this).parents("li").prevAll().each(function(){
      var el = $($(this).children("a").attr("href"));
      
      left_offset -= el.width() + parseFloat(el.css("margin-left")) + parseFloat(el.css("margin-right"));
    });
    
    $("#work").css({ "margin-left": left_offset }, 200);
    
    $("#work article").removeClass("active");
    $($(this).attr("href")).addClass("active");
    
    
    if($("#progress .active").index() + 1 === $("#progress li").size()){
      $("#next a").text("<");
    }else{
      $("#next a").text(">");
    }
    
    
    back_button_clicked = false;
  });
  
  var first_click = true;
  
  window.onpopstate = function(e){

  }
  

  
     $("#blog .post").css("cursor", "pointer").click(function(){ document.location = $(this).find("a").attr("href"); });
});

jQuery(document).ready(function($){ 
if($("body").hasClass('page-template-static-php')){
	 $("#next").remove();
	 $(".arrows").remove();
}


$.reject({  
	  reject : { 
        all: false, 
        msie5: true,
		msie6: true,
		msie7: true,
		msie8: false,
		firefox2: true,
		safari2: true,
		safari3: false
        /* 
            Possibilities are endless... 
             
            msie: false,msie5: true,msie6: true,msie7: false,msie8: false, // MSIE Flags (Global, 5-8) 
            firefox: false,firefox1: false,firefox2: false,firefox3: false, // Firefox Flags (Global, 1-3) 
            konqueror: false,konqueror1: false,konqueror2: false,konqueror3: false, // Konqueror Flags (Global, 1-3) 
            chrome: false,chrome1: false,chrome2: false,chrome3: false,chrome4: false, // Chrome Flags (Global, 1-4) 
            safari: false,safari2: false,safari3: false,safari4: false, // Safari Flags (Global, 1-4) 
            opera: false,opera7: false,opera8: false,opera9: false,opera10: false, // Opera Flags (Global, 7-10) 
            gecko: false,webkit: false,trident: false,khtml: false,presto: false, // Rendering Engines (Gecko, Webkit, Trident, KHTML, Presto) 
            win: false,mac: false,linux : false,solaris : false,iphone: false, // Operating Systems (Win, Mac, Linux, Solaris, iPhone) 
            unknown: false // Unknown covers everything else 
        */  
    },
	display: ['firefox','chrome','safari','msie'], // Displays only custom "Flock" browser, safari and opera.  
	browserInfo: {  
		firefox: { // Specifies browser name and image name (browser_flock.gif)  
		text: 'Firefox 7+', // Text Link  
		url: 'http://www.mozilla.com/firefox/' // URL To link to  
}  
}  
});  
          
return false;  
          
});


