////////////////////////////////////////
// Over Label
////////////////////////////////////////
jQuery.fn.overlabel = function() {
    this.each(function(index) {
        var label = $(this); var field;
        var id = this.htmlFor || label.attr('for');
        if (id && (field = document.getElementById(id))) {
            var control = $(field);
            label.addClass("overlabel-apply");
            if (field.value !== '') {
                label.not('.readonly').css("display", "none");
            }
            control.focus(function () {label.not('.readonly').css("display", "none");}).blur(function () {
                if (this.value === '') {
                    label.css("display", "block");
                }
            });
            label.click(function() {
                var label = $(this); var field;
                var id = this.htmlFor || label.attr('for');
                if (id && (field = document.getElementById(id))) {
                    field.focus();
                }
            });
        }
    });
};

$(document).ready(function(){
    
  
  
    //  Location Hash
    var locationHash = window.location.hash;
    if (locationHash != '') {
      if($(locationHash).length) {
        $(locationHash).show().siblings().hide();
        $('#slideshow-link').css({"color":"#999999"});
        // $('#slideshow-link.grey').css({"color":"#999999"});
        $('a[href=' + locationHash +']').css({"color":"#9F1F13"});
      }
    }
    $('ul.infoLinks li a').each(function() {
      // $('#slideshow-link').css({"color":"#9F1F13"});
      $('#slideshow-link.grey').css({"color":"#999999"});
      var $this = $(this);
      $this.click(function() {
        $(this).css({"color":"#9F1F13"});
        $(this).parent().siblings().children().css({"color":"#999999"});
        
        window.location.hash = $this.attr('href')
        
        return false;
      });
    });
  // external links
  $('a[rel=external]').click(function(){
    window.open(this.href);
    return false;   
  });
  
  // Image Fade
  //$('#homeSlide img').hide();
  
  // Accordian Nav
  $('ul.accordian>li>a').click(function(){
    if ($(this).parents('li').hasClass('open')) {
      $(this).parents('li').removeClass('open').find('ul').slideUp();
    } else {
      $(this).parents('li').siblings()
      .removeClass('open').find('ul').slideUp()
      .end()
      .end()
      .end()
      .parents('li')
      .addClass('open').find('ul').slideDown();
    }
  }); 
  
  
  // Slide Shows
  if(jQuery.fn.cycle){
    var filmslides = $('#filmslides div').length;
    var slides = $('#slide img, .bigSlide img').length;
    var itemPic = $('.itemPic img').length;
    var html = ' ';
    var htmlfilm = ' ';

    html += '<div class="slideControls">';
    html += '<span class="prev"><a href="#">&lt;</a></span>';
    html += '<span class="count"><span>1</span> / '+ slides +'</span>';
    html += '<span class="next"><a href="#">&gt;</a></span>';
    html += '</div>';
    
    htmlfilm += '<div class="slideControls">';
    htmlfilm += '<span class="prev"><a href="#">&lt;</a></span>';
    htmlfilm += '<span class="count"><span>1</span> / '+ filmslides +'</span>';
    htmlfilm += '<span class="next"><a href="#">&gt;</a></span>';
    htmlfilm += '</div>';
    
    function updateCount(idx, slide) {
      $('.count span').text(slide + 1);
      var html = $('#slide div:eq('+ slide +')').find('p').html();
      $('.slideDesc').html(html);
    }
    
    if (slides > 1) {
      $('#slide').after(html).cycle({
        fx: 'fade',
        timeout: 0,
        speed: 500,
        prev: '.prev',
        next: '.next',
        prevNextClick: updateCount
        // before : updateCaption
      });
      
      $('.bigSlide').cycle({
        fx: 'fade',
        timeout: 3000,
        speed: 1000
      });
      
    }
    
    if (filmslides > 1) {
      $('#filmslides').after(htmlfilm).cycle({
        fx: 'fade',
        timeout: 0,
        speed: 500,
        prev: '.prev',
        next: '.next',
        prevNextClick: updateCount
      });
      
    }
    
    if (itemPic > 1) {
      $('.itemPic').cycle({
        fx: 'fade',
        timeout: 0,
        speed: 500,
        prev: '.prev',
        next: '.next',
        pager : '.itemTypes',
        pagerAnchorBuilder: function(idx, slide) { 
          return '.itemTypes li:eq(' + idx + ') a'; 
        }
      });
      
    }

  }
  
  // Thumb Overlays
  if(jQuery.fn.fancybox){
    $('.imageGrid li a').fancybox({
      overlayOpacity : .85,
      padding : 0
    });
    $('a.box').fancybox({
      overlayOpacity : .85,
      padding : 20,
      frameWidth : 400,
      frameHeight : 580,
      hideOnContentClick : false, 
      callbackOnShow : function(){
        $('label').overlabel(); 
      }
    });
  }
  
  // Forms
  if (jQuery.fn.overlabel) {
    $('#order-form label, #contact-form label, #clientLogin label').overlabel();
  }
  
  
  // News Feed
  $('.feed a.thumb, #shopItems li a.thumb').hover(function(){
    $(this).parents('li').addClass('hover');
  }, function(){
    $(this).parents('li').removeClass('hover'); 
  });
  

  // Flexi Pagination
  if(jQuery.fn.flexiPagination){
    $(".design-log").flexiPagination({
      url: "/design-log?ajax=1",
      currentPage: 1,
      totalResults: 10000,
      perPage: 2,
      container: ".design-log",
      pagerVar : "p",
      loaderImgPath: "/includes/style/images/loader.gif",
      debug : 0
    });
  };
  
  //Show / Hide photographer Bio
  
  $('#photo-bio-link').bind('click', function(){
    $('#slideshow').hide();
    $('.imageGrid').hide();
    $('#photo-films').hide();
    $('#photo-bio').show(); 
    // return false; 
  });
  
  $('#photo-films-link').bind('click', function(){
    $('#slideshow').hide();
    $('.imageGrid').hide();
    $('#photo-bio').hide();
    $('#photo-films').show();
  });
  
  $('#slideshow-link').bind('click', function(){
    $('#photo-bio').hide();
    $('#photo-films').hide();
    $('#photo-images').show();
    $('#slideshow').show();
    $('.imageGrid').show();
  });
  
  //jQ Validate
  $('#contact-form').validate();
  $('#order-form').validate();
  $('.lightbox-submit').live('click', function(){
    $('#contact-form').validate();
  });
  
  
  //Blog post Images caption
  $('.blog-post img').each(function(){
    $(this).after("<p class='blog-caption'>"+$(this).attr("title")+"</p>");
  });
  
  
  // Same As Button
  $('#sameAs').click(function() {
    fields = new Array('name','company','address','address_2','city','state','state_txt','country','phone','zip');
      for (var q in fields) {
        document.getElementById('shipping_'+fields[q]).value = document.getElementById('billing_'+fields[q]).value;
      }
      jQuery("#shipping_country").trigger('change');
  });
  
  // Cart Functions
  
  /*
  $('.qtAdjust .more').click(function(){    
    
    qt = $(this).parent().parent().siblings('.qt');   
    if (parseInt(qt.html()) < $(this).attr('title')){
      qt.html(parseInt(qt.html())+1);
      
      $.post("cart_qty_up.php", {
          row_id: $(this).attr('alt'),
          qty: $(this).attr('title')          
      }); 
      
      var $single_price = $(this).parent().parent().parent().siblings('.price').children('.item-total');
      var single_price = $single_price.html().replace(/,/g,'');
      
      var $price = $(this).parent().parent().parent().siblings('.price').children('.currency');    
      var price = $price.html();    
      var s_price = price.split('$');
      var r_price = s_price[1].replace(/,/g,'');
      
      var $sub_total = $('#subTotal').find('.currency');
      var sub_total = $sub_total.html();
      var r_total = sub_total.replace(/,/g,'');
      
      var new_price = parseFloat(r_price) + parseFloat(single_price);
      var new_total = parseFloat(r_total) + parseFloat(single_price);
      
      var t_price = new_price.toFixed(2);
      var t_total = new_total.toFixed(2);
      
      if(t_price>1000){
        var act_price = t_price.substring(0, 1) + ',' + t_price.substring(1);      
      }
      else{
        var act_price = t_price;
      }
      if(t_total>1000){
        var act_total = t_total.substring(0, 1) + ',' + t_total.substring(1);
      }
      else{
        var act_total = t_total;
      }
      
      $price.html('$'+act_price);
      $sub_total.html(act_total);      
    }
    else{
      $('.max-qty').show();
    }
  });
  
  
  
  $('.qtAdjust .less').click(function(){
          
    qt = $(this).parent().parent().siblings('.qt');   
   
    if (parseInt(qt.html()) > 1){
      
      $.post("cart_qty_down.php", {
          row_id: $(this).attr('alt')                 
      });
      
      qt.html(parseInt(qt.html())-1);
      
      var $single_price = $(this).parent().parent().parent().siblings('.price').children('.item-total').html();
      var single_price = $single_price.replace(/,/g,'');
      
      var $price = $(this).parent().parent().parent().siblings('.price').children('.currency');    
      var price = $price.html();    
      var s_price = price.split('$');
      var r_price = s_price[1].replace(/,/g,'');
      
      var $sub_total = $('#subTotal').find('.currency');
      var sub_total = $sub_total.html();
      var r_total = sub_total.replace(/,/g,'');
      
      var new_price = parseFloat(r_price) - parseFloat(single_price);
      var new_total = parseFloat(r_total) - parseFloat(single_price);
      
      var t_price = new_price.toFixed(2);
      var t_total = new_total.toFixed(2);
      
      if(t_price>1000){
        var act_price = t_price.substring(0, 1) + ',' + t_price.substring(1);      
      }
      else{
        var act_price = t_price;
      }
      if(t_total>1000){
        var act_total = t_total.substring(0, 1) + ',' + t_total.substring(1);
      }
      else{
        var act_total = t_total;
      }
      
      $price.html('$'+act_price);
      $sub_total.html(act_total);
    }
  });
  
  
  $('a.remove-item').click(function(){
    var mp = $(this).parents('tr'),
      items = $('#shoppingCart tbody tr').length;

    var $price = $(this).parent().siblings('.price').children('.currency'),
      price = $price.html(),
      s_price = price.split('$'),
      r_price = s_price[1].replace(/,/g,'');

    var $sub_total = $('#subTotal').find('.currency'),
      sub_total = $sub_total.html(),
      r_total = sub_total.replace(/,/g,'');

    var new_total = parseFloat(r_total) - parseFloat(r_price),
      t_total = new_total.toFixed(2);
    

    if (items <= 1) {
      $('#shoppingCart').remove();
      $('#page').append('<div class="verb-store-cart-empty">Your cart is empty!</div>');  
    } else {
      mp.remove();
    }     

    $.post("cart_remove_item.php", {
      row_id: $(this).attr('alt')
    }); 
    
    if(t_total>1000){
      var act_total = t_total.substring(0, 1) + ',' + t_total.substring(1);
    }
    else{
      var act_total = t_total;
    }
    $sub_total.html(act_total); 
  }); 
  
  $('.sold-out').click(function(){
    return false;
  });
  
  */
  
//.....................................................
// New Store Section
//.....................................................

    // Slideshow
    $('#itemSlides').cycle({ 
      speed: 1000,
      timeout: 0,
      easein: 'easeInCubic',
      easeout: 'easeOutCubic', 
      pager:  '#slideThumbs', 
      pagerAnchorBuilder: function(idx, slide) { 
        // return selector string for existing anchor 
        return '#slideThumbs li:eq(' + idx + ') a'; 
      }     
    }); 
     
    // Slides <-> Select Options
    $('#addtocart #optionSelect').change(function() { 
      var thisId = $("#optionSelect option:selected").attr('id');
      $('#slideThumbs').find("#"+thisId).trigger('click');
      $('#switchPrice').empty().append($('#optionSelect option:selected').html());
      return false; 
    });
    
    $('#slideThumbs a').click(function(){
      var thisId = $(this).attr('id');
      var optId = $('#addtocart #optionSelect option[id='+thisId+']');
      if(optId.attr('value') != null){
        $(optId).attr('selected', 'selected');
        $('#switchPrice').empty().append($('#optionSelect option:selected').html());
      }
    });
    
    // Fade Slides in on load
    $('#itemSlides .slide, #page #itemSlides .slide p').bind('load', function(e) {
        $(this).animate({
          'opacity': 1
        });
    });

});


//.....................................................
// Win Load
//.....................................................

$(window).load(function() {
    $('#itemSlides .slide.first, #itemSlides .slide.first p').animate({
      'opacity': 1
    });
});

