/*
*
*	common.js
*	- Common JS
*
*/

$(document).ready(function() {	
	fadeNav();
	if(isIe6()){
		$('#navigation').supersleight();
		$('#content #primary ul li').supersleight();
		$('.scrollContainer li').supersleight();
		$('#content ul.cat-entries li.panel').supersleight();
		$('div.member').supersleight();
	}
	if(!isIe6()){
		if($('#home-news').size() > 0){
			scoll_home_news();
		}
	}
	initSlider();
	highlight_sires()
	doAccordion(300);
	//showHide('#show-filters', '#filters');
	validateCommentForm();
	if($('#back-to-top').size() > 0){
		$('#back-to-top').localScroll();
	}
		
});
/*
*
*	Fades in the sub nav
*
*/
function fadeNav(){
	$('#nav-main li.active li, #nav-main li.active').each(function(i){
		$('.'+this.className).css('opacity', '0').animate({
			opacity: 1.0
		}, (500*i));
		//console.log($('.'+this.className).text());
	})	
}


/*
*
*	Custom accordion for anything
*	Click handler toggles it's next sibling
*/

function doAccordion(speed){
	$('ul.accordion .togContent').hide();
	$('ul.accordion .toggler').css('cursor', 'pointer');
		$('ul.accordion .toggler').bind('click', function(){
			var thisItem = $(this);
			$('ul.accordion .togContent').slideUp(speed);
			$('ul.accordion .toggler').parent().removeClass('active');
			thisItem.parent().toggleClass('active');
			thisItem.siblings('.togContent').toggle(speed);
		});
}

function initSlider(){
	if($('#pony-scroller').size() > 0){
		if(build_slider_nav($('#pony-scroller ul').size())){
			$('.pony-slider').addClass('slideable');
			//$('#slider').before('<h2 class="no-top-margin no-border">Latest entries</h2>');		
			$('.with-sub-nav.catalogue-home #wrapper-outer').css('background', '#00112c url("/_img/_common/tile-bg-cat-home.png") repeat-x');	
			$("#slide-controller").jFlow({
			    slides: "#pony-scroller",
				controller: ".slide-control", // must be class, use . sign
			    width: "885px",
			    height: "270px",
				easing: "easeOutQuint",
			    duration: 1000,
				prev: ".slide-prev", // must be class, use . sign
				next: ".slide-next" // must be class, use . sign
		
			});
		}
		$('span.prev a').addClass('inactive');
		$('span.next a').addClass('active');
	}
}

/**
 *	Builds the nav for the slider
**/
function build_slider_nav(num_pages){
		
		var nav_width;
		
		nav  = '<div id="slide-controller">';
		nav += '<span class="prev"><a href="#" title="Previous page" id="grid_prev" class="slide-prev"></a></span>';
		nav += '<ul id="slide-nav" class="slide-controls">';
		
		for(var i=1; i<num_pages+1; i++){
			nav += '<li><a href="#" title="Go to page '+i+'" class="slide-control">'+i+'</a></li>';
		}
		nav += '</ul>';
		nav += '<span class="next"><a href="#" title="Next page" id="grid-next" class="slide-next"></a></span>';
		nav += '</div>';
		
		nav_width = num_pages*29;
		
		if($(nav).appendTo($('#slider'))){
			$('#slide-nav').css('width',nav_width);
			return true;
		}else{
			return false;
		}
}

/*
*	SHow hide content
*	@param	toggler
*	@param  target
*/

function showHide(toggler, target){
	$(toggler).css('display', 'block');
	$(target).hide();
	
	var isUp = true;
	
	//console.log(innerHtml)
	
	$(toggler).click(function(){
		switch(isUp){
			case true:
				$(target).slideDown(500, function(){
					isUp = false;
					$(toggler +' a').html('Hide browsing options').attr('title', 'Hide browsing options');;
				});
			break;
			case false:
				$(target).slideUp(500, function(){
					isUp = true;
					$(toggler +' a').html('More browsing options').attr('title', 'More browsing options');
				});
			break;
		}
		return false;
	});
}

/*
*	Validates the comments form
*/

function validateCommentForm(){
	if($("#commentform").size() > 0){
		$("#commentform").validate({
		  	errorPlacement: function(error, element) {
		    	error.hide().insertBefore(element.parent()).fadeIn('slow');
		   	},
			errorElement: "p",
			wrapper: "",
			messages: {
				author: {
					required: "Please enter your name",
					minlength: jQuery.format("At least {0} characters required!")
				},
				email: {
					required: "Please enter a valid email address",
					minlength: jQuery.format("At least {0} characters required!")
				},
				comment: {
					required: "Please enter your comment",
					minlength: jQuery.format("At least {0} characters required!")
				}
			}
		});		
	}		
}

/*
*	Checks if browser is IE6
*/
function isIe6(){
	if( $.browser.msie && ($.browser.version < 7) ){
		return true;
	}else{
		return false;
	}
}

/**
 * Decodes entites.
 */
function decodeEntities(s){
    return s.replace(/&amp;/g,  "&")
            .replace(/&quot;/g, '"')
            .replace(/&quot;/g, "'")
            .replace(/&lt;/g,   "<")
            .replace(/&gt;/g,   ">")
			.replace(/&#226;/g, "â")
			.replace(/&#226;/g, "â")
			.replace(/&#234;/g, "ê")
			.replace(/&#234;/g, "ê")
			.replace(/&quot;/g, "‘")
			.replace(/&quot;/g, "’")
			.replace("\"");
};
/*
*	Scroll news items on the home page
*/
function scoll_home_news(){
	$('#home-news .post').hide()
	var post_html = '';
	
	$.getJSON("/_sidebars/sidebar.home-news.php?showcount=3&json_format=1",
		function(data){
			$.each(data.newsitems, function(i,item){
				
				post_html += '<div class="post_inner">';
				post_html += '<h3><a href="'+item.link+'" title="'+item.title+', read the full post">'+item.title+'</a></h3>';
				post_html += '<h4 class="date">'+item.date+'</h4>';
				post_html += item.excerpt;
				post_html += '<p class="action right"><a href="'+item.link+'" rel="bookmark" title="'+item.title+', read the full post" class="action">Read more</a></p>';
				post_html += '</div>';	
			});
			//console.log(data);
			$('#home-news .post').html(post_html).fadeIn(500, function(){
				$('#home-news .post').cycle({ 
				    fx:     'scrollUp', 
				    timeout: 10000, 
				    easeIn:        null,  // easing for "in" transition 
				    easeOut:       null,  // easing for "out" transition
					pause: 1
				});
			});
			
	  });
	
}

/*
*	Checks if browser is IE6
*/
function fade_banner(){
	$('#banner-ads a').mouseover(function(){
		$(this).fadeOut(500).css('background-position', 'bottom left').fadeIn(500)
	})
	$('#banner-ads a').mouseout(function(){
		$(this).fadeOut(500).css('background-position', 'top left').fadeIn(500)
	})
}

function highlight_sires(){
	$('.wp-table-reloaded td').each(function(){
		if($(this).text()!==""){
			$(this).addClass('sire');
		}
	});
}