$(function(){

	$(".tweet").tweet({
		username: "Source4Style",
		count: 1,
		loading_text: "loading tweets...",
		template: "{text}{time}"
	});		   
	
	$(".SectionImg div").delay(600).animate({top: "30",opacity: "show"}, "slow");
	
	$(".OtherLinks a").hover(function(){
	  $('span', this).stop().animate({left: "30"}, "slow");
	}, function() {
	  $('span', this).stop().animate({left: "15"}, "slow");
	});		
	
	function resizeMsg(){
		var winHeight = $(window).height();
		// var percentage = 0.6278;		
		var percentage = 0.6278;	
		var newHeight = Math.round(winHeight * percentage);		
		var innerHeight = newHeight - 110;
		$('.teaser').height(newHeight);
		$('.InnerTeaser').height(innerHeight);
	}
	
	$(window).resize(function(){
		resizeMsg();
	});
		   
	resizeMsg();
	
	
	$('.expandable').hover(function(){		
		  $('#MainNav ul').hide();
		  if($('body').attr('id') == 'home'){
			  if($(this).attr('id') == "MenuTrends"){
					$('#header').stop().animate({  height: "430" }, 'slow');
			  }else{
					$('#header').stop().animate({  height: "380" }, 'slow');
			  }
		  } else {
			  if($(this).attr('id') == "MenuTrends"){
					$('#header').stop().animate({  height: "315" }, 'slow');
			  }else{
					$('#header').stop().animate({  height: "265" }, 'slow');
			  }
		  }
		  $('ul',this).fadeIn();
		  if($('> a',this).attr('class') != 'current'){
			  $('> a',this).addClass('active');
			  Cufon.refresh();
		  }
		  return false;

	}, function(){
		$('.expandable a').removeClass('active');
		Cufon.refresh();
		$('#MainNav ul').fadeOut();
		if($('body').attr('id') == 'home'){
			$('#header').stop().animate({  height: "140" }, 'slow');	
		} else {
			$('#header').stop().animate({  height: "100" }, 'slow');	
		}
			
		return false;		
	});

	
	$('#MainNav ul li[class!="current"]').animate({"opacity" : .4});
	$('#MainNav ul li[class!="current"]').hover(function(){
		
		$(this).stop().animate({"opacity" : 1});
	
	}, function(){
	
		$(this).stop().animate({"opacity" : .4});
	
	});
	
	if ($.browser.msie && $.browser.version.substr(0,1)<7) {
	 	$('.item').hover(			
			function(){
				$(this).addClass('ie6hover');
			}
			,function(){
				$(this).removeClass('ie6hover');
		});
	}
	
	$('#ItemColors a').hover(
			function(){
				$('#color').text($(this).attr('title'));
				var gallery = $(this).attr('href');
				$('.gallery').hide();
				$(gallery).show();
				console.log(gallery);
			},
			function(){
				$('#color').text($('#ItemColors .current').attr('title'));
				var gallery = $('#ItemColors .current').attr('href');
				$('.gallery').hide();
				$(gallery).show();
			}
	);
	
	$('#ItemColors a').click(
			function(){
				$('#ItemColors a').removeClass('current');
				$(this).addClass('current');
				return false;
			}
	 );
	
	$('.pictures').cycle({
		timeout: 0,
		pager:  '.thumbs',
		pagerAnchorBuilder: function(idx, slide) { 		
		   return '.thumbs li:eq(' + idx + ') a'; 
		} 
	});	
	
	var countTabs = $('.tabs li').size();
	$('.tabs li').each( function(){
		$(this).css('z-index', countTabs);
		countTabs--;
	});
	$('.TabContent').hide();
	$('.TabContent:first').show(); 
	$('.tabs li:first').addClass('active');
	$('.tabs.unlocked li a').click(function(){
		$('.tabs li').removeClass('active'); 
		$(this).parent().addClass('active'); 
		Cufon.refresh();
		var currentTab = $(this).attr('href'); 
		$('.TabContent').hide(); 
		$(currentTab).show();
		return false;
	});
	
	$('.Press, #colors li,.BubbleTouch').toggle(
		function(){
			$('.checklist-container').hide();
			$(this).addClass('active');
		},
		function(){
			$('.checklist-container').hide();
			$(this).removeClass('active');
		}
	);

	var menuopen = false;	
	
	$('html').click(function() {
	  $('.checklist-container').hide();
	});
	
	$('.checklist-container').click(function(event){ event.stopPropagation(); });
	
	$('.Combo').click(		
		function(event){
			
			 event.stopPropagation(); 
			 
			if(!menuopen && $(this).next('.checklist-container').is(':hidden')){
				menuopen = true;
				$(this).next('.checklist-container').show();
				
			}
			else if(menuopen && $(this).next('.checklist-container').is(':visible')){
				menuopen = false;
				$(this).next('.checklist-container').hide();					
				
			} else if(menuopen && $(this).next('.checklist-container').is(':hidden')){
				menuopen = true;
				$('.checklist-container').hide();
				$(this).next('.checklist-container').show();
			}

		}
	);
	
	

	
	$('.checklist-options .checkall').click(function () {
		$(this).parents('li:eq(0)').find(':checkbox').attr('checked', this.checked);
		
		$(this).parents('li:eq(0)').find(':checkbox').map(function() { 
		  if(this.checked){
			 $('.ComboSelections .selection:contains("'+this.value+'")').show();
		  } else {
			  $('.ComboSelections .selection:contains("'+this.value+'")').hide();
		  }
		}); 
		
	});
	
	$('.firstlevel input').click(function () {
		
		var numCheckBoxes = $(this).parents('.firstlevel:eq(0)').find('ul input').size(); 
		
		var numChecked = $(this).parents('.firstlevel:eq(0)').find('ul input:checked').size();
		
		var getvalue = $(this).val();
		
		var attr = $(this).parents('ul:eq(0)').attr('class');
															   
		if($(this).parents('.firstlevel:eq(0)').find('.checkall').attr('checked') == 'checked' && attr != 'firstlevel'){		
			$(this).parents('.firstlevel:eq(0)').find('.checkall').attr('checked', this.checked);
			
			var checkallValue = $(this).parents('.firstlevel:eq(0)').find('.checkall').val();
			$('.ComboSelections .selection:contains("'+checkallValue+'")').hide();

		}
		
		if( numCheckBoxes == numChecked && attr != 'firstlevel') {
			$(this).parents('.firstlevel:eq(0)').find('.checkall').attr('checked', this.checked);
			
			console.log(attr);

			var checkallValue = $(this).parents('.firstlevel:eq(0)').find('.checkall').val();
			$('.ComboSelections .selection:contains("'+checkallValue+'")').show();

		}
		
		if($(this).is(':checked')){			
			$('.ComboSelections .selection:contains("'+getvalue+'")').show();

		} else {
			$('.ComboSelections .selection:contains("'+getvalue+'")').hide();
		}
		
		if($(this).parents('.checklist-options').find('input:checked').size() > 0){
			$(this).parents('.checklist-widget').find('.Combo span').text('Filtered');
		} else {
			$(this).parents('.checklist-widget').find('.Combo span').text('- Any -');
		}

	});
	
	
	$('.ComboSelections .remove').click(function(){
		
		var hasChildren = $(this).parents('li:eq(0)').children('ul').length;
		var hasParent = $(this).parents('li:eq(1)').length;
		var content = jQuery.trim($(this).parent().text());

		
		if(!hasChildren && !hasParent){ //si no tiene padre ni hijos
			$(this).parent().hide();
			$(this).parents('.FieldHolder').find('.checklist-options :checkbox[value = "'+content+'"]').attr('checked', false);			
		}			
		
		if(hasChildren){ //si tiene hijos se va el padre y los hijos
		
			$(this).parents('li:eq(0)').find('.selection').hide();
			$(this).parents('.FieldHolder').find('.checklist-options :checkbox[value = "'+content+'"]').parents('li:eq(0)').find(':checkbox').attr('checked', false);	
			
		}
		if(hasParent){ //si es hijo, se debe ir el padre cuando ya no hayan hijos
			$(this).parent().hide();
		 	var siblings = $(this).parents('ul:eq(0)').find('.selection:visible').length; //numero de hermanos visibles
			$(this).parents('.FieldHolder').find('.checklist-options :checkbox[value = "'+content+'"]').attr('checked', false);	
			
			if(siblings == 0){
				$(this).parents('li:eq(1)').children('.selection').hide(); //se va el padre cuando no hay hijos
				$(this).parents('.FieldHolder').find('.checklist-options :checkbox[value = "'+content+'"]').parents('li:eq(1)').find('.checkall').attr('checked', false);	
			}			
		}
		
		if($(this).parents('.FieldHolder').find('.checklist-options input:checked').size() > 0){
			$(this).parents('.FieldHolder').find('.checklist-widget .Combo span').text('Filtered');
		} else {
			$(this).parents('.FieldHolder').find('.checklist-widget .Combo span').text('- Any -');
		}
		
		return false;

		

	});
	
	$('.ToggleFolders').toggle(
			
			function(){
				$('span',this).text('Show Folders');
				$(this).animate({
					left: '-8px'
				  });
				$('.SwatchbookFolders').animate({
					left: '-232'
				  },function(){
					  $('#FoldersHolder').hide();
					  $('.ItemHolder').width('936');
				  });
				return false;
			},
			
			function(){
				$('span',this).text('Hide Folders');
				$('.ItemHolder').width('703');
				 $('#FoldersHolder').show();
				$(this).animate({
					left: '224'
				  });
				$('.SwatchbookFolders').animate({
					left: '0'
				  });
				return false;
				
			}
	);
	
	$('.ToggleFilters').toggle(
			
			function(){
				$('strong',this).text('Show Filters');
				$('#filters table').animate({opacity: "hide"}, "slow");
				$('#filters').slideUp();
				return false;
			},
			
			function(){				
				$('#filters table').animate({opacity: "show"}, "slow");	
				$('#filters').slideDown();
				$('strong',this).text('Hide Filters');
				return false;
				
			}
	);
	
	$('.ResetFilters').click(function(){			
			$('.selection').hide();
			$('.Combo span').text('- Any -');
			$('.checklist-container').hide();
			//$('.BtnTouch,.BubbleTouch,#colors li').removeClass('active');
			$('.BtnTouch.active,.BubbleTouch.active,#colors li.active').click();
			$('.checklist-options input').attr('checked', false);
			$("#slider-range").slider("option", "values",  [ 0, 60 ] );
			$("#button-range").slider("option", "values",  [ 0, 704 ] );
			$( ".amount" ).text("$0 - Above $50" );
			return false;
		 });
	
	//Select all anchor tag with rel set to tooltip
    $('.tipx').mouseover(function(e) {
         
        //Grab the title attribute's value and assign it to a variable
        var tip = $(this).attr('title');   
         
        //Remove the title attribute's to avoid the native tooltip from the browser
        $(this).attr('title','');
         
        //Append the tooltip template and its value
        $("body").append('<div id="tooltip">' + tip + '</div>');    
         
        //Set the X and Y axis of the tooltip
		//var tipH = $('#tooltip').width() / 2;
        $('#tooltip').css('top', e.pageY + 18 );
        $('#tooltip').css('left', e.pageX - 10 );
         
    }).mousemove(function(e) {
     
        //Keep changing the X and Y axis for the tooltip, thus, the tooltip move along with the mouse
        $('#tooltip').css('top', e.pageY + 18 );
        $('#tooltip').css('left', e.pageX - 10 );
         
    }).mouseout(function() {
     
        //Put back the title attribute's value
        $(this).attr('title',$('#tooltip').text());
     
        //Remove the appended tooltip template
        $('#tooltip').remove();
         
    });
	
	function equalHeight(group) {
		var tallest = 0;
		group.each(function() {
			var thisHeight = $(this).height();
			if(thisHeight > tallest) {
				tallest = thisHeight;
			}
		});
		group.height(tallest);
	}
	
    equalHeight($(".plan .post")); 
	
	
	
	 $('.faq .handle').live('click',function(){
		  var dd = $(this).next();
		
		  if(!dd.is(':animated')){
			 
			  dd.slideToggle();
			  $(this).toggleClass('opened');
		  }

	  });
	 
	 $('#ShowBilling').click(function(){ $('#BillingAddress').toggle(); });
	
	
	$('#ChooseCC').click(function(){
		
		if($(this).attr('checked') == "checked"){$('#MethodCreditCard').show(); $('#MethodPayPal').hide();}
		
	});
	
	$('#ChoosePayPal').click(function(){
		
		if($(this).attr('checked') == "checked"){$('#MethodPayPal').show(); $('#MethodCreditCard').hide();}
		
	});
	
});
