/* Author: 
	Jean-Baptiste Louvet - http://www.jblouvet.com
*/

var isIpad = navigator.userAgent.match(/iPad/i) != null;

function isIphone(){
    return (
        (navigator.platform.indexOf("iPhone") != -1) ||
        (navigator.platform.indexOf("iPod") != -1)
    );
}

jQuery(document).ready(function($) {
  		
  		var h = $('body').height(),
  			w = $(window).height();
  		
  		if (h <= w) { $('#main').css('min-height', parseInt(h/1.7)); }
  		
  		if ( isIphone() ) {
  			$('#cart-widget-links a.gocheckout').text('OK');
  		}
  		
  	     $('input.wpsc_buy_button').click(function() {
  	          if($(this).parents('form:first').find('select.wpsc_select_variation[value=0]:first').length) { // if have variations and not selected prevent from clicking through
  	               return false;
  	          }
  	         var itemCount = parseInt($('#hide-show-basket em.count').text()); // selects the current count of the header cart items
  	         var itemQuantity = parseInt($(this).parents('#single_product_page_container, table.list_productdisplay td, #products_page_container .productcol').find('input[name=wpsc_quantity_update]').val());
  	         if (itemQuantity > 1) {
  	               itemCount += itemQuantity;
  	          } else {
  	               itemCount++;
  	          }
  	          $('#hide-show-basket em.count').text(itemCount);
  	     });
  		
  		$('.alt-empty').live('click',function(){
  			//var itemCount = $('#hide-show-basket em.count').text();
  			//itemCount.text('0');
  			$('#sliding_cart .remove_button').trigger('click');
  		});
  		
	/* VARS */
  	var html = $('html'),
  		body = $('body');
  
	/* INIT */
	$('#widget-area-1').hide();
	
	//$('p > img').unwrap();
	
	/* BASKET */
	$('#hide-show-basket').click(function(){
		var voir = 'voir mon ',
			cacher = 'cacher le ';
		if ( $('span',this).is('.voir') ) {
			$('span',this).removeClass('voir').addClass('cacher').text(cacher)
		} else {
			$('span',this).removeClass('cacher').addClass('voir').text(voir)
		}
		$('#widget-area-1').slideToggle(300); }
	);
	
	$('#product-header .wpsc_buy_button').click(function(){
		console.log('submitted');
		$('#hide-show-basket').trigger('click');
	});
	
	/* HOME */
	if ( body.is('.home') ) {
		
		/* homepage logo link to */
		$('#banner h1 a').attr('href','/revue');
		
		/* homepage fancybox */
		$('.fancyhome').fancybox({'padding':0}).trigger('click');
		
		/* homepage background-image */
		var homeBG = $('article').data('homebg');
		
			body.css({
				'background': '#000 url('+homeBG+') no-repeat center top'
			});
		
		if (Modernizr.backgroundsize) {
		
			body.css({
				'backgroundSize':'cover',
				'-o-background-size':'cover',
				'-moz-background-size':'cover',
				'-webkit-background-size':'cover',
				'-ms-background-size':'cover'
			});
			
		}
		
	}
	
	
	/* BLOG */
	if ( body.is('.blog') ) {
		
		var $titles = $('h1.entry-title a'),
			$articles = $('#content article');
		
		$titles.each(function(){ $(this).hover(function(){ $(this).css('color','black');}).removeAttr('href'); });
		
		if ( html.not('.oldie') ) {
			
			/* chargement ajax des articles */
			var content = '#content',
				nav = '#nav-below',
				next = '#nav-below .nav-previous a',
				nextHref = $(next).attr('href'),
				_More = '+ d\'articles ? ',
				_Loading = '...chargement...',
				_noMore = 'La Fin';
		
			//console.log($('#content article').length);
			
			if ( $articles.length == 10 ) {
				$(nav).html('<a id="more-posts" href="' + nextHref + '">' + _More + '</a>');
			}
			
			$('#more-posts').click(function(e) {
			
				e.preventDefault();
				
				$(this).text(_Loading);
				
				$.get($(this).attr('href'), function(data) {
				
					var $timestamp = new Date().getTime(),
						$posts = $(content,data).wrapInner('<div class="loaded-posts" id="posts-' + $timestamp + '" />').html();
						nextHref = $(next, data).attr('href');
						
						$('html,body').animate({scrollTop: $(nav).position().top}, 'slow');
						$(nav).before($posts);
						
						$('#posts-' + $timestamp).hide().fadeIn('slow');
						$('.loaded-posts ' + nav).remove();
						if (nextHref !== undefined ) {
							$('#more-posts').attr('href', nextHref).text(_More);
						} else {
							$(nav).html('<p id="no-more-posts">'+_noMore+'</p>');
						}
				});
			});
		}
	}
	
	/* PRODUCT LIST */
	if ( body.is('.page-revue') ) {
	
		if ( html.is('.csstransitions') ) {
	
			var productsItem = $('.default_product_display');
			
			productsItem.hover(function(){
				$(this).parent().children().removeClass('hovered').addClass('not-hovered')/*.css('opacity','0.2')*/;
				$(this).removeClass('not-hovered').addClass('hovered')/*.css('opacity','1')*/;
			},function() {
				$(this).parent().children().removeClass('not-hovered').addClass('hovered')/*.css('opacity','1')*/; 		
			});
			
		} else {
		
			var productsItem = $('.default_product_display');
			productsItem.hover(function(){
				$(this).parent().children().css('opacity','0.2');
				$(this).css('opacity','1');
			},function() {
				$(this).parent().children().css('opacity','1'); 		
			});
		
		}
	}
	
	/* SINGLE PRODUCT */
	
	/* CONTRIBUTEURS */
	
	var $contributeur = $('#contributeurs h2');
	
	$contributeur.click(function(){
		var $contribution = $(this).next('p').not('.v'),
			$bloc = $(this).parent('div'),
			$contributions = $('p.v');
			$blocs = $('div.bloc');
		
		$contributions.removeClass('v').slideUp('100');
		$blocs.removeClass('v').addClass('not-v');
		$contribution.addClass('v').slideDown('1000');
		$bloc.removeClass('not-v').addClass('v');
	});
	
	/* GALERIE */
	
	$('.fancybox, .thickbox').fancybox({
				padding  : 0,
				closeBtn : false,
				helpers		: { 
					title	: { type : 'outside' }
				},
				afterLoad : function() { this.title = '<em>photo : </em>' + $('#main .wpec_gallery').find('img').eq(this.index).attr('alt'); }
	});
	
	/* ????? */
	
	$('.preview_link').click(function(e) {
		$('.wpec_gallery').children('a:first').trigger('click');
		return false
	});
	
	/* PRODUITS ASSOCIES */
	
	$('.wpsc-latest-products li').hover(function(){
		$(this).find('img').css('opacity','0.5');
	},function(){
		$(this).find('img').css('opacity','1');
	});
	
	
	/* POINTS DE VENTE */
	
	if ( body.is('.page-id-27') ) {
	
		var $pdvNav = $('#pdv-nav'),
			$pdvHeader = $('article > header',this),
			$pdv = $('#pdvs');
		
		/* init */
		
		if ( !isIphone() ) {
			$pdvNav.css({
				'position':'fixed',
				'top':'260px'
			});
			$pdvHeader.css({
				'position':'fixed',
				'top':'180px'
			});
		} else {
			$pdvHeader.hide();
				$pdvNav.css({
					'position':'fixed',
					'top':'205px'
				});
			
		}
		
		$('li ul', $pdvNav).hide();
		
		$pdvNav.localScroll({offset:-200});
		
		/* on click, scroll */
		
		$('a',$pdvNav).click(function(){
			$('a.active',$pdvNav).removeClass('active');
			$(this).addClass('active');
		});

		
	}
	
	
	/*CHECKOUT*/
	
	if ( isIphone() ) {
		$('#checkout_page_container form.adjustform input[type="submit"]:first').val('ok');
		$('#checkout_page_container form.adjustform input[type="submit"]:last').remove();
	}
});






















