// Company JavaScript Document

$(function() {
	
	$('#featured ul li .article-content').fadeOut(1);
	
	$.lts.navigation();
	$.lts.rotate();
	$.lts.popup();
	$.lts.tips();
	$.lts.share();
	
	shoppingCartSetUp();
	
	if ($(".date-picker").length > 0) {
		$(".date-picker").datepicker({
				showWeek: true, 
				firstDay: 1,
				changeMonth: true,
				yearRange: 'c-100:c+10',
				changeYear: true,
				constrainInput: true, 
				dateFormat: 'dd M yy'
				});
	}

	
	$('.form .required label').html(function(i, html) {return html + ' <span class="req">*</span>'});
	
	$('.related-products li').click(
		function() { 
		window.location = $(this).find("a").attr("href");
	
	});

	$('.navigation ul li a').addClass('toplevel');
	$('.navigation ul li ul li a').removeClass('toplevel');
	
	$('.navigation ul li').each(function() {
		if ($(this).find('ul').length > 0) {
			$(this).addClass('has-children');
		}
	});
	
	if ($(".succes").length > 0) {
		$("body").addClass("body-form-message");
	}
	
	if ($("#video").length > 0) {
		$("#video").carousel({ loop: true, pagination: true });
	}
	
	$('.content-highlight').hover(
		function(){		
			//alert('mmm');
			$(this).find('.tabs').animate({ 
				top: 0
			  }, 150 );
		},
		function() {
			//alert('kay');
			$(this).find('.tabs').animate({ 
				top: -40
			  }, 90 );
			
		}
	);	
	
	$('.rotate').hover(
		function(){
			$(this).addClass('hover');
			$('.rotate li .article-content').fadeIn(500);

			
		},
		function() {
			$(this).removeClass('hover');
			$('.rotate li .article-content').fadeOut(500);
		});	
	
	Cufon.replace($('.navigation ul li a.toplevel'), {hover: true});
	Cufon.replace($('ul.tabs li a'), {hover: true});
	Cufon.replace($('.panel-header h1'));
	Cufon.replace($('.panel-header h2'));
	Cufon.replace($('.panel-header h3'));
	
	//paginationSlider();
	gridRollovers();
	smallgridRollovers();

	var tabContainers = $('div.content-highlight div.section');
    
	tabContainers.hide().eq(0).show();
                        
    $('div.content-highlight ul.tabs li a').click(function () {
        tabContainers.hide();
		tabContainers.filter(this.hash).show();
		$('div.content-highlight ul.tabs li a').removeClass('selected');
		$(this).addClass('selected');
		return false;
	}).eq(0).click();
	

	//var tabContainers2 = $('.sub .panel-content .section');
	
	$('.sub .panel').each(function() {

		if ( $(this).find('.panel-header .icons').length > 0 ) {
    
			var sections = $(this).find('.section');
			sections.hide().eq(0).show();
			
			$(this).find('.panel-header ul.icons li a').click(function () {
				
				sections.hide();
				sections.filter(this.hash).show();
				$(this).parent().parent().find('a').removeClass('selected');
				$(this).addClass('selected');
				return false;
				
			}).eq(0).click();
			
			/*					
			$('.sub .panel-header ul.icons li a').click(function () {
				parent.hide();
				parent.filter(this.hash).show();
				$('.sub .panel-header ul.icons li a').removeClass('selected');
				$(this).addClass('selected');
				return false;
			}).eq(0).click();
			*/
	
		}
		
	});
	
	
	/*
	
	var tabContainers3 = $('.sub .twitter .panel-content .section');
    
	tabContainers3.hide().eq(0).show();
                        
    $('.sub .twitter .panel-header ul.icons li a').click(function () {
        tabContainers3.hide();
		tabContainers3.filter(this.hash).show();
		$('.sub .twitter .panel-header ul.icons li a').removeClass('selected');
		$(this).addClass('selected');
		return false;
	}).eq(0).click();
*/

	
	if ($(".gallery_demo_unstyled").length > 0) {

			$('.gallery_demo_unstyled').addClass('gallery_demo').addClass('clear');			
			
			$('ul.gallery_demo li').each(function(idx) {
				$(this).data('index', (++idx));
			});
		
		   var car = $('ul.gallery_demo').jcarousel({
				scroll: 5,
				initCallback: initCallbackFunction
			})
	
				
			$('ul.gallery_demo li:first').addClass('active');
			
		   // window.setInterval(function(){
			//    $.galleria.next()
			//}, 3000) // must be set in miliseconds
		 
			// $('#slideshow').fadeIn();
				
			$('ul.gallery_demo').galleria({
										  
									  
											
					history   : false, // activates the history object for bookmarking, back-button etc.
					insert: '.product-image .main-image',
					
					onImage   : function(image,caption,thumb) { // let's add some image effects for demonstration purposes
				
				
				// fade in the image & caption
				if(! ($.browser.mozilla && navigator.appVersion.indexOf("Win")!=-1) ) { // FF/Win fades large images terribly slow
					image.css('display','none').fadeIn(300);
				}
				caption.css('display','none').fadeIn(1000);
				
				// fetch the thumbnail container
				var _li = thumb.parents('li');
				
				// fade out inactive thumbnail
				_li.siblings().children('img.selected').fadeTo(500,0.3);
				
				// fade in active thumbnail
				thumb.fadeTo('fast',1).addClass('selected');
				
				// add a title for the clickable image
				image.attr('title','Next image >>');
			},
			onThumb : function(thumb) { // thumbnail effects goes here
				
				// fetch the thumbnail container
				var _li = thumb.parents('li');
				
				// if thumbnail is active, fade all the way.
				var _fadeTo = _li.is('.active') ? '1' : '0.3';
				
				// fade in the thumbnail when finnished loading
				thumb.css({display:'none',opacity:_fadeTo}).fadeIn(300);
				
				// hover effects
				thumb.hover(
					function() { thumb.fadeTo('fast',1); },
					function() { _li.not('.active').children('img').fadeTo('fast',0.3); } // don't fade out if the parent is active
				)
			}
				});
			
			}
			
			
			
		function initCallbackFunction(carousel) {
				$('#img').bind('image-loaded',function() {
					var idx =  $('ul.gallery_demo li.active').data('index') - 2;
			   //     alert('j');
					carousel.scroll(idx);
					return false;
				});
			};
	
	
	});


function shoppingCartSetUp() {
	
	$('.navigation-account li ul').each(function() {
		newheight = 0 - $(this).height()-140;
		$(this).css('top', newheight);
		$(this).addClass("TEST");
	});
	

	$('.navigation-account li a.basket-link').click(
														
		function() {
			//return false;
		});
		   
	
	$('.navigation-account li').hover(											
							   
		function() { 
		
			$(this).addClass('menu-over');
			
			$(this).find('ul').css("top", "100%");	
		},
		
		function() { 
			
			$(this).removeClass('menu-over');
			origheight = 0 - $(this).find('ul').eq(0).height()-140;	
			$(this).find('ul').css("top", origheight);				 
	});

}


function paginationSlider(url) {
	
	//	$('.content .shop-header .paging').css('display', 'none');
	//	$('.content .shop-footer .paging').css('display', 'none');
		
		$('.layout-homepage .pagination').css('display', 'none');
	
	
	
	if ($(".layout-homepage .pagination a.next").length > 0) {
		
		var mainUlposts = $("ul.latest-posts");
		
		if ($(".products-ajax-wrap").length == 0) {
			mainUlposts.wrap('<div class="products-ajax-wrap" />');
		}
		
		var mainUlHposts = parseInt(mainUlposts.css('height')) + 28 + 50;
		$('.products-ajax-wrap').css('overflow', 'visible').css('height', mainUlHposts);
		
		if (url == null) {
			url = $(".layout-homepage .pagination a.next").attr('href');
		}
		
		
		mainUlposts.append($('<li />')
					.addClass('load-more')
					.html(
						$('<a />')
							.html('Load more posts')
							.click(function() { 
								pagingloadposts(url, mainUlposts);	
							})
				));
	}
	
	
	
	
	if ($("div.paging a.nexts").length > 0) {
			
		var mainUl = $("ul.products");
		
		if ($(".products-ajax-wrap").length == 0) {
			mainUl.wrap('<div class="products-ajax-wrap" />');
		}
		
		var mainUlH = parseInt(mainUl.css('height')) + 28 + 50;
		$('.products-ajax-wrap').css('overflow', 'visible').css('height', mainUlH);
		
		
		if (url == null) {
			url = $("div.paging a.next").attr('href');
		}
		
		mainUl.append($('<li />')
					.addClass('load-more')
					.html(
						$('<a />')
							.html('Load more products')
							.click(function() { 
								pagingloadpage(url, mainUl);			
							})
				));
		
	}
}



function pagingloadpage(url, into) {
	
	var results;
	
	$.ajax({
		   
	  beforeSend: showLoader,
	  url: url,
	  cache: false,
	  success: function(html){
		  
		results = html;
		  
		products = $(results).filter('.products');
		
		products = products.find('li').addClass('li-ajax');
		
		nextLink = $(results).find('.next').length;
	
		$('ul.products li.loader-inline').fadeOut('slow', function() {
			
			$('ul.products li.loader-inline').remove();
			
			into.append(products);	
			
			$(".content ul.products li.li-ajax").css('visibility', 'visible'); 
			
			$(".content ul.products li.li-ajax").animate({ 
				opacity: 1,
			}, 1000 );
			
			$(".content ul.products li.li-ajax").removeClass('li-ajax');
			
			newHeight = into.height() + 28 + 50;
		
			$(".products-ajax-wrap").animate({ 
				height: newHeight,
			}, 1000, function() {
				
				if (nextLink > 0) {
					paginationSlider($(results).find('.next').eq(0).attr('href'));
				}
				
			} );
							
		 });
		
	  }
	  
	  
	});
}




function pagingloadposts(url, into) {
	
	var results;
	
	$.ajax({
		   
	  beforeSend: showLoaderPosts,
	  url: url,
	  cache: false,
	  success: function(html){
		  
		results = html;
		  
		products = $(results).find('.latest-posts');
		
	//	alert(products.html());
		
		//products = $(products).find('li').addClass('li-ajax');
		
		nextLink = $(results).find('.next').length;
	
		$('ul.latest-posts li.loader-inline').fadeOut('slow', function() {
			
			$('ul.latest-posts li.loader-inline').remove();
			
			into.append(products.html());	
			$.lts.share();
			
			$(".content ul.latest-posts li.li-ajax").css('visibility', 'visible'); 
			
			$(".content ul.latest-posts li.li-ajax").animate({ 
				opacity: 1,
			}, 1000 );
			
			//$(".content ul.latest-posts li.li-ajax").removeClass('li-ajax');
			
			newHeight = into.height() + 28 + 50;
		
			$(".products-ajax-wrap").animate({ 
				height: newHeight,
			}, 1000, function() {
				
				if (nextLink > 0) {
					paginationSlider($(results).find('.next').eq(0).attr('href'));
				}
				
			} );
							
		 });
		
	  }
	  
	  
	});
}

function showLoaderPosts() {
	
	$('ul..latest-posts li.load-more').remove();
	$('ul..latest-posts').append('<li class="loader-inline"><span class="bg"></span><span class="text"><img src="/theme/gumball/images/loader-small.gif" /> Loading</span></li>');
}

function showLoader() {
	
	$('ul.products li.load-more').remove();
	$('ul.products').append('<li class="loader-inline"><span class="bg"></span><span class="text"><img src="/theme/gumball/images/loader-small.gif" /> Loading</span></li>');
}

function gridRollovers() {
	
	$('.grid li .grid-overlay').hover(
						   
	function() { 
	
		$(this).find('.grid-content').animate({ 
        top: 134
      }, 100 );
	}
	,
	
	function() { 
		$(this).find('.grid-content').animate({ 
        top: 200
      }, 100 );

	});
	
	$('.grid li').click(
		function() { 
		window.location = $(this).find("a").attr("href");
	
	});
	
}

function smallgridRollovers() {
	
	$('.grid-small li').hover(
						   
						   
	function() { 
		$(this).find('a').animate({ 
        top: 93
      }, 100 );
	}
	,
	
	function() { 
		$(this).find('a').animate({ 
        top: 140
      }, 100 );

	});
	
	$('.grid-small li').click(
		function() { 
		window.location = $(this).find("a").attr("href");
	
	});
	
}


