jQuery(document).ready(function($) {


    // Remove no-js class from html attribute if JavaScript is enabled
    $('html').removeClass('no-js');
	
	
	// Make favorites sortable
	$( ".mfp_favorites" ).sortable();
	$( ".mfp_favorites" ).disableSelection();
	

	// Init tabs
	$( "#tabs" ).tabs();
	

	// Hide product title on load
	$('.product_title, .related_post_title').animate({opacity: 0, bottom:'-10px'}, 0);
	$('.product_link, .product_fav, .product_fav_r, .product_cart').animate({opacity: 0, left:'-100px'}, 0);
	

	// Hide blog info on load 
	var heightX = $('.blog_title').height();
	var heightY = $('.blog_title h3').height();
	var resaultZ = heightX - heightY;

	$('.blog_title').animate({bottom:'-'+resaultZ+'px'}, 0);
	$('.comment-reply-link').animate({bottom: '-50px', opacity: '0'},0);

	// Ajax page navigation (overriden by WP)
	jQuery('#posts-navigation a').live('click', function(e){
    e.preventDefault();

    var link = jQuery(this).attr('href');
    var height = jQuery('#ajax-container').height();
	
    jQuery('#blog-page').css('height', 'auto');
    jQuery('#ajax-container').fadeOut(500).load(link + ' #ajax-inner', function(){ jQuery('#ajax-container').fadeIn(500); });
	});

	jQuery('#product-navigation a').live('click', function(e){
    e.preventDefault();
	
    var link = jQuery(this).attr('href');
    var height = jQuery('#ajax-container').height();
	
    jQuery('#mainContent').css('height', 'auto');
    jQuery('#ajax-container').fadeOut(500).load(link + ' #ajax-inner', function(){ jQuery('#ajax-container').fadeIn(500); });
	});

	
	// Grab simpleCart values
	$('#quantity').hover(function() {
		var quant = $('#shopHold .simpleCart_quantity').text();
		$(this).attr('title','You have '+quant+' item(s) in your cart. ')
	});
	

	// Product Hover
	var lis = $(".product_wrap");
	$(lis).hover(
	
	  function () {
		$(this).find('.product_title').stop(true,false).animate({opacity: 1, bottom:'5px'}, 500, 'easeInOutQuad');
		$(this).find('.product_link').stop(true,false).animate({opacity: 1, left:'5px'}, 500, 'easeInOutQuad');
		$(this).find('.product_fav, .product_fav_r').stop(true,false).delay(100).animate({opacity: 1, left:'5px'}, 500, 'easeInOutQuad');
		$(this).find('.product_cart').stop(true,false).delay(200).animate({opacity: 1, left:'5px'}, 500, 'easeInOutQuad');
	  },
	  
	  function () {
		$(this).find('.product_title').stop(true,false).animate({opacity: 0, bottom:'-50px'}, 500, 'easeInOutQuad');
		$(this).find('.product_link').stop(true,false).animate({opacity: 0, left:'-100px'}, 500, 'easeInQuad');
		$(this).find('.product_fav, .product_fav_r').stop(true,false).delay(100).animate({opacity: 0, left:'-100px'}, 500, 'easeInQuad');
		$(this).find('.product_cart').stop(true,false).delay(200).animate({opacity: 0, left:'-100px'}, 500, 'easeInQuad');
	  }
	  
	);

	// Blog Hover 
	var heightX = $('.blog_title').height();
	var heightY = $('.blog_title h3').height();
	var resaultZ = heightX - heightY;

	$('.blog-post').hover(function() {
		$(this).find('.blog_title').stop(true,false).animate({bottom:'-5px'}, 500, 'easeInOutQuad');
	},
	function () {
		$(this).find('.blog_title').stop(true,false).animate({bottom:'-'+resaultZ+'px'}, 500, 'easeInOutQuad');
	});

	
	// Blog comments reply link animation
	$('.comment').hover(function() {
		$(this).find('.comment-reply-link').stop(true,false).animate({opacity:'1', bottom: '-35px'}, 500, 'easeInOutQuad');

	}, function() {

		$(this).find('.comment-reply-link').stop(true,false).delay(600).animate({opacity:'0', bottom: '-50px'}, 500, 'easeInOutQuad');

	});

	
	// Related Product Hover
	var lis = $(".related_posts");
	
	$(lis).hover(
	  function () {
		$(this).find('.related_post_title').stop(true,false).animate({opacity: 1, bottom:'5px'}, 500, 'easeInOutQuad');
	  },
	  
	  function () {
		$(this).find('.related_post_title').stop(true,false).animate({opacity: 0, bottom:'-50px'}, 500, 'easeInOutQuad');
	  }
	  
	);

	
	// Initialize menu
	 $("ul.sf-menu").supersubs({minWidth:13, maxWidth:30, extraWidth:0}).superfish({hoverClass:'sfHover', pathClass:'sf-active', pathLevels:0, delay:500, animation:{height:'show'}, speed:'def', autoArrows:0, dropShadows:0}) ;

	 
	// Clear cookie after purchase
	function del_cookie(name) {
        document.cookie = name + "=; expires=Thu, 01-Jan-70 00:00:01 GMT;";
    }
	
	$('.simpleCart_shelfItem .buyButton').click(function () {
		del_cookie("simpleCart");
	})
	
	
	// Tooltips
	$('.bag').tipsy({gravity: 'n', opacity: 1, offset: 20, html:true});
	$('#quantity').tipsy({gravity: 'n', opacity: 1, offset:55, html:true});
	$('#paypal').tipsy({gravity: 'w', opacity: 1, offset: 5});
	$('.product_title span').tipsy({gravity: 's', opacity: 1, offset: 12});
	$('.tagcloud a').tipsy({gravity: 's', opacity: 1, offset: 3, fade: true, opacity: 0.9, delayIn: 300});
	$('.product_link, .product_fav, .product_fav_r, .product_cart').tipsy({gravity: 'w', opacity: 1, offset: 5});
	$('#tabs-1 select').tipsy({gravity: 'w', opacity: 1, offset: 5, trigger: 'focus'});
    $('.widget_calendar td a').tipsy({gravity: 'w', opacity: 1, offset: 5});
	

	// Go to top			
	$('#logo').click(function(){ 		
	$('html, body').animate({scrollTop:0},2000);	
	});
	

	// Infield Text (Clear Input on focus)
	$('input[type=text],textarea, input[type=password], input[type=email], input[type=search]').click(
		function () {
			if (this.value == this.defaultValue) {
			this.value = '';
		}
	});

	$('input[type=text],textarea, input[type=password], input[type=email], input[type=search]').blur(	
		function () {
			if (this.value == '') {
			this.value = this.defaultValue;
		}
	});
	
	
	// Dialog Window on Add To Cart method
	$(document).ready(function() {  
 
    $('.product_wrap .item_add').click(function(e) {
		var name = $(this).parent().find('.product_title .item_name').text();
		var price = $(this).parent().find('.product_title .item_price').text();
		
		$('#dialog #dialog_pr_name').text(name);
		$('#dialog #dialog_pr_price').text(price);
		
        //Cancel the link behavior
        e.preventDefault();
		
		// Scroll to top
		$('html, body').animate({scrollTop:0},1000);
		
        //Get the A tag
        var id = $('#dialog');
     
        //Get the screen height and width
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();
     
        //Set height and width to mask to fill up the whole screen
        $('#mask').css({'width':maskWidth,'height':maskHeight});
         
        //transition effect        
        $('#mask').fadeTo("medium",0.9);  
     
        //Get the window height and width
        var winH = $(window).height();
        var winW = $(window).width();
               
        //Set the popup window to center
        $(id).css('top',  winH/2-$(id).height()/2);
        $(id).css('left', winW/2-$(id).width()/2);
     
        //transition effect
        $(id).fadeIn(1000); 
     
    });
     
    //if close button is clicked
    $('.window .closeDialog, #closePanelBtn').click(function (e) {
        
		// Cancel the link behavior
        e.preventDefault();
        $('#mask, .window').hide();
    });     
     
	 // if ok button is clicked
    $('#okPanelBtn').click(function (e) {    
		$('#mask, .window').hide();
    });  
	 
    // if mask is clicked
    $('#mask').click(function () {
        $(this).hide();
        $('.window').hide();
    });         
     
});

// Now lets do the same for single_product.php file
$(document).ready(function() {  
 
    $('#buySection .item_add').click(function(e) {
		var name = $('.item_name').text();
		var price = $(this).parent().find('.item_price').text();
		
		$('#dialog #dialog_pr_name').text(name);
		$('#dialog #dialog_pr_price').text(price);
		
        //Cancel the link behavior
		e.preventDefault();
		
		// Scroll to top
		$('html, body').animate({scrollTop:0},1000);
        
		//Get the A tag
        var id = $('#dialog');
     
        //Get the screen height and width
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();
     
        //Set height and width to mask to fill up the whole screen
        $('#mask').css({'width':maskWidth,'height':maskHeight});
         
        //transition effect        
        $('#mask').fadeTo("medium",0.9);  
     
        //Get the window height and width
        var winH = $(window).height();
        var winW = $(window).width();
               
        //Set the popup window to center
        $(id).css('top',  winH/2-$(id).height()/2);
        $(id).css('left', winW/2-$(id).width()/2);
     
        //transition effect
        $(id).fadeIn(1000); 
     
    });
     
    //if close button is clicked
    $('.window .closeDialog, #closePanelBtn').click(function (e) {
        
		//Cancel the link behavior
        e.preventDefault();
        $('#mask, .window').hide();
    });     
     
	 //if ok button is clicked
    $('#okPanelBtn').click(function (e) {    
		$('#mask, .window').hide();
    });  
	 
    //if mask is clicked
    $('#mask').click(function () {
        $(this).hide();
        $('.window').hide();
    });         
     
});

/*  Contact Form Processing */  
		var flagSent = 0;
  $('#submit1').live( 'click', function() {
  if(flagSent != 0 ) {return false;}
  else {
		flagSent = 5;
		var email = $('#email').val();	
		var name   = $('#name').val();
		var message = $('#message').val();
		var mailo = $('#mailo').val();
		
		$(this).attr('value', 'message sent. thank you.');
		$(this).addClass('messageSent');
		
		var dataString = '&mailo='+ mailo + '&name=' + name + '&message=' + message + '&email=' + email;
		
		
		
			$.ajax({
			type: "POST",
			url: "wp-content/themes/fluencetheme/contactform.php",
			data: dataString,
			success: function() {
			var hght = $(window).height();
			var wdth = $(window).width();
			var msg1h = $('.successMessage').height();
			var msg2w = $('.successMessage2').width();
			
			var hghtR1 = (hght-msg1h)/2;
			var wdthR1 = (wdth-msg2w)/2;
			
			$('.successMessage2').css('top',hghtR1-25+'px');
			$('.successMessage2').css('left', wdthR1+'px');
			$('.successMessage2').fadeIn().delay(5000).fadeOut('slow');
			}
		});
  
  return false;
		
}
  });



}); // End jQuery
