/**
 * VARIABLES
 *
 */
    var debug = false;
    
/**
 * ISSET
 *
 * PHP style isset() method for variables and objects.
 */
    function isset(variable)  {
    	try {
    		if(typeof(eval(variable)) != 'undefined')
    		if(eval(variable) != null) return true;
    	} catch(e) {}
    	return false;
    }


/**
 * TRACE
 *
 * Browser safe console logging method.
 */
    function trace(msg) {
        try { window.console.log(msg) } catch (err) {}
    }


/**
 * INIT
 *
 * Init objects
 */
    $(document).ready(function(){
        init_video();
        init_carousels();
        if($("#work").size() > 0)  init_scroll(); //only use infinite scroll on work page
        //strip_tel_links();
        $(".mobile-nav").click(function(){
            $(this).remove();
            $("#header ul:hidden").show();
        });
        
    });
    
    


/**
 * VIDEO
 *
 */
    
    function init_video() {
        $("#intro").fitVids({ customSelector: "iframe" });
    }
    
    function remove_video(){
    
    }



/**
 * CAROUSELS
 *
 */
                             
	function init_carousels() {
	    
	    function swipe(event, direction) {
		    switch(direction){
		        case "left":
		            $(event.target).parent().parent().siblings().filter(".slidesNext").click();
		            break;
		        
		        case "right":
	                $(event.target).parent().parent().siblings().filter(".slidesPrevious").click();
		            break;
		    }
		}
	    
	    
	    
	    $(".slides").each(function(){
	        if($(this).children().length > 1) {
	            $(this).slides({
              responsive: true,
        			pagination: false,
        			fade: { interval: 0 },
         			slide: { interval: 500 }
        		}).swipe({
        		  swipe:swipe,
        		  threshold:0,
        		  allowPageScroll: "vertical"
        		});
	        } else {
	            $(this).children().show();
	        }
	    })
		
		$(".next").live("click", function(){
		    $(this).parent().parent().next("section").find(".slidesNext").click();
		    return false;
		});
		
		$(".prev").live("click", function(){
		    $(this).parent().parent().next("section").find(".slidesPrevious").click();
		    return false;
		});
		
    }



/**
 * INFINITE SCROLL
 *
 */
  function init_scroll() {
    
    var timeout = null;
    
    // On Scroll
    $(window).scroll(function(){

      // If if the user has scrolled near the bottom of the page
      if ( $("#work").offset().top + $("#work").height() + 82 <= $(document).scrollTop() + $(document).height()){    
        
        if(timeout)     clearTimeout(timeout);
        
        timeout = window.setTimeout(function(){
          
          post_count++;
          
          // If there are pages to load
          if(work_post_ids.length > post_count) { //there are more IDs available than we are showing
           
            
            // Show spinner
            var _loader = $("#work #loader");
            _loader.show();
            
            var _pid = work_post_ids[post_count];
            
            //check the pid is valid
            if(_pid != "undefined" && !isNaN(_pid)){  
                  
              $.get("/wp-content/themes/traffic/ajax/get_work.php?pid="+ _pid, function(data){
                
                $("#work #loader").before(data);
                                
                var _this = $(".work:last .slides");
                     
                _loader.hide();
              });
            }
          }
          timeout = null;
      }, 750);
      }
    });
  }  

/**
 * MOBILE
 *
 */ 
 
  //remove tel links for desktop (requires mobile esp lib)
  function strip_tel_links(){
    //if not browsing on a mobile device
    if(!DetectMobileQuick()){
      
      //replace the link with plain text
      $("a").filter(function(){ $(this).attr("href").search(/tel/i); }).replaceWith($(this).text());
    }
  }

