function setdj(passed,daynight){
	jQuery('#djcol').removeClass('carousel-detail-top-night').removeClass('carousel-detail-top-day').addClass('carousel-detail-top-'+daynight);
	jQuery("#slider-list-djout").createPipeCarousel(passed,2);
}
function setmusic(passed,daynight){
	jQuery('#musiccol').removeClass('carousel-detail-top-night').removeClass('carousel-detail-top-day').addClass('carousel-detail-top-'+daynight);
	if (passed == 'kube'){
		jQuery("#slider-list-musicout").createPipeCarousel(passed,3);
	} else {
		jQuery("#slider-list-musicout").createPipeCarousel(passed,2);
	}
}
function setmedia(passed,daynight){
	jQuery('#mediacol').removeClass('carousel-detail-top-night').removeClass('carousel-detail-top-day').addClass('carousel-detail-top-'+daynight);
	jQuery("#slider-list-mediaout").createPipeCarousel(passed,2);
}

function setnews(passed,daynight){
	jQuery('#newscol').removeClass('carousel-detail-top-night').removeClass('carousel-detail-top-day').addClass('carousel-detail-top-'+daynight);
	if (passed == 'kube'){
		jQuery("#slider-list-newsout").createPipeCarousel(passed,3);
	} else {
		jQuery("#slider-list-newsout").createPipeCarousel(passed,2);
	}
	
}


jQuery(document).ready(function($) {
	$("#slider-djlist").createPipeCarousel("djlist",5,'djs');
	$("#slider-medialist").createPipeCarousel("medialist",5,'media');
	$("#slider-musiclist").createPipeCarousel("musiclist",5,'music');
	
	
	// singles
	$("#slider-list-compout").createPipeCarousel('comp',2);
	$("#slider-list-newsout").createPipeCarousel("news",2);
	$("#slider-list-eventsout").createPipeCarousel("events",2);
	
	Cufon.replace(".moveable-top h3");
});
			
			
			
			
var totalkiss = window.totalkiss || {}; // namepsace

/**
 *  JQuery plugin
 */
jQuery.fn.createPipeCarousel = function(passed,colnum,isasub) {
	// this, parent, prev, next, colnum, submenuitem
	var pipeFeed = new totalkiss.PipeFeed(this,jQuery(this).parent().attr("id"),jQuery(this).parent().prev().attr("id"),jQuery(this).parent().next().attr("id"),colnum,isasub);
	jQuery(this).css('left', "0px");
	jQuery(this).html('<div style="width:736px; text-align:center; padding-top:30px;"><img src="/wp-content/themes/totalkiss/images/loading-small.gif" alt="Loading..." width="32" height="32"/></div>');
	pipeFeed.parse(passed);
};

/**
 *  PipeFeed Class
 *  Returns an ordered list of feed items
 */
totalkiss.PipeFeed = function(htmlElement,tocarousel,prev,next,colnum,isasub) {
	//htmlElement.html('<ol id="feed-results"></ol>');
	//var listElement = $("#slider-list");
	
	return {
		parse : function(passedval) {
			jQuery.getJSON('/wp-content/themes/totalkiss/scripts/pipes.php',{ passed: passedval }, function(json) {
					parseFeed(json);
				}
			);
		}
	};
	
	function parseFeed(json) {
		if(json.count > 0) {
			jQuery(htmlElement).empty();
			 jQuery(json.value.items).each( function() {
				var itemDetail = createTitle(this) + createContent(this);
				// set the number / width of the cols
				var widthval = 0;
				switch (colnum){
					case 2:
						widthval = 365;
						break;
					case 3:
						widthval = 243;
						break;
					case 4:
						widthval = 182;
						break;
					case 5:
						widthval = 145;
						break;
					case 6:
						widthval = 121;
						break;
					default:
						widthval = 243; // 3
						break;
				}
				
				// 
				if (!readCookie('djcarousel')){
					var djs = 'djs';
				} else {
					var djs = urldecode(readCookie('djcarousel')).split(',');
				}
				// if (String(this.station).toLowerCase() == 'network');
				
				
				
				//if (!readCookie('mediacarousel')){
					var media = 'all';
				//} else {
					//var media = urldecode(readCookie('mediacarousel')).split(',');
				//}
				
				//if (!readCookie('musiccarousel')){
					var music = 'all';
				//} else {
					//var music = urldecode(readCookie('musiccarousel')).split(',');
				//}
				
				
				if (isasub){ // sub menu set get the cookie array
					var tocheck = eval((isasub));
					var isok = jQuery.inArray(String(this.nicename), eval(isasub));
					// alert (eval(isasub));
					if (eval(isasub) == 'all'){
						isok = true;
					} else if (eval(isasub) == 'djs'){
						// get station
						if (!readCookie('kiss')){
							var stationcookie = '100';
						} else {
							var stationcookie = readCookie('kiss');
						}
						
						// is a network show = all good
						if (String(this.station) == 'Network'){
							isok = true;
						} else if (String(this.station).indexOf(stationcookie)!= -1){
							isok = true;
						}
					}
					
					if (isok != -1){
						jQuery(htmlElement).append("<div class='slide-item' style='width:"+widthval+"px;' >" + itemDetail + '</div>');
					}
				} else if (!isasub){
					jQuery(htmlElement).append("<div class='slide-item' style='width:"+widthval+"px;' >" + itemDetail + '</div>');
				}
			});
			// make the carousel
			jQuery("#"+tocarousel).kiss_carousel("#"+prev,"#"+next);
			// start off the first item if it has the correct javascript call
			var jscall = jQuery("#"+tocarousel+" .sub-nav-option:first").attr("href");
			if (String(jscall) != 'undefined'){
				eval(jscall);
			}
			Cufon.replace("#"+tocarousel+" .slide-item h4");
	     } else {
			htmlElement.html("The request did not return results");
	     }		
	}
	
	function createTitle(item) {
		if (isasub){
			var type = "";
			switch(String(item.daynight)){
				case 'day':
					type = "sub-nav-option-day";
					break;
				case 'night':
					type = "sub-nav-option-night";
					break;
				case 'both':
					type = "sub-nav-option-both";
					break;
				default:
					type = "sub-nav-option";
					break;
			}
			return '<h4><a title="'+item.title+'" href="' + item.link + '" class="sub-nav-option ' + type + '">' + unescape(item.title) + '</a></h4>';
		} else {
			
			var titletext = "";
			
			// has the story an image?
			var theimage ='';
			if (String(item.gg) != 'undefined'){
				theimage = String(item.gg);
			} else if (String(item.image) != 'undefined' && String(item.image) != 'null' ){
				theimage = String(item.image);
			}
			
			var imagewidth = 0;
			if (colnum == 2){
				var imagewidth = 142;
			} //else if (colnum == 3){
				//var imagewidth = 100;
			//}
			
			if (theimage != "" && imagewidth != 0){
				// can we use tim thumb?
				
				if (theimage.indexOf('timthumb') != -1){
					titletext+= '<a title="'+item.title+'" href="' + item.link + '"><img class="item-image" src="'+theimage+'" width="'+imagewidth+'px" height="142px" /></a><div class="item-content'+colnum+'">';
				} else if (theimage.indexOf('djs.totalkiss.com') != -1){
					titletext+= '<a title="'+item.title+'" href="' + item.link + '"><img class="item-image" src="http://djs.totalkiss.com/wp-content/themes/kissbyday/scripts/timthumb.php?zc=1&w='+imagewidth+'&h=142&src='+theimage+'" width="'+imagewidth+'px" height="142px" /></a><div class="item-content'+colnum+'">';
				} else if (theimage.indexOf('breakfast.totalkiss.com') != -1){
					titletext+= '<a title="'+item.title+'" href="' + item.link + '"><img class="item-image" src="http://breakfast.totalkiss.com/wp-content/themes/kissbreakfast/scripts/timthumb.php?zc=1&w='+imagewidth+'&h=142&src='+theimage+'" width="'+imagewidth+'px" height="142px" /></a><div class="item-content'+colnum+'">';
				} else if (theimage.indexOf('breakfast101.totalkiss.com') != -1){
					titletext+= '<a title="'+item.title+'" href="' + item.link + '"><img class="item-image" src="http://breakfast101.totalkiss.com/wp-content/themes/kissbreakfast/scripts/timthumb.php?zc=1&w='+imagewidth+'&h=142&src='+theimage+'" width="'+imagewidth+'px" height="142px" /></a><div class="item-content'+colnum+'">';
				} else if (theimage.indexOf('breakfast105.totalkiss.com') != -1){
					titletext+= '<a title="'+item.title+'" href="' + item.link + '"><img class="item-image" src="http://breakfast105.totalkiss.com/wp-content/themes/kissbreakfast/scripts/timthumb.php?zc=1&w='+imagewidth+'&h=142&src='+theimage+'" width="'+imagewidth+'px" height="142px" /></a><div class="item-content'+colnum+'">';
				} else if (theimage.indexOf('totalkiss.com') != -1){
					titletext+= '<a title="'+item.title+'" href="' + item.link + '"><img class="item-image" src="http://www.totalkiss.com/wp-content/themes/totalkiss/scripts/timthumb.php?zc=1&w='+imagewidth+'&h=142&src='+theimage+'" width="'+imagewidth+'px" height="142px" /></a><div class="item-content'+colnum+'">';
				} else {
					titletext+= '<a title="'+item.title+'" href="' + item.link + '"><img class="item-image" src="'+theimage+'" width="'+imagewidth+'px" height="142px" /></a><div class="item-content'+colnum+'">';
				}
			}
			
			titletext+= '<h4><a title="'+item.title+'" href="' + item.link + '">' + item.title + '</a></h4>';
			
			return titletext;
		}
	}

	function createContent(item) {
		// get the author
		var theauthor = "Totalkiss";
		if (item.author != 'admin'){
			theauthor = item.author;
		}
		if (theauthor == 'kube'){
			theauthor = "Kube";
		}
		
		// max text length - dependent on the number / width of the cols
		var maxlen = 0;
		switch (colnum){
			case 2:
				maxlen = 230;
				break;
			case 3:
				maxlen = 130;
				break;
			case 4:
				maxlen = 60;
				break;
			default:
				maxlen = 100; // 3
				break;
		}
		if (colnum == 2 && (String(item.gg) != 'undefined' || (String(item.image) != 'undefined' && String(item.image) != 'null' ))){
			maxlen = 80;
		}
		if (colnum == 3 && (String(item.gg) != 'undefined' || (String(item.image) != 'undefined' && String(item.image) != 'null' ))){
			maxlen = 50;
		}
		var description = trim(item.description);
		// reduce output if necessary
		if (description.length > maxlen){
			description = trim(description.substr(0,(maxlen-5)))+"...";
		}
		
		var itemContent = '<p class="ksc-carousel-tease">' + description + '</p><div class="clear">';
		
		// add author if there is one
		if (String(theauthor) != 'undefined'){
			itemContent += '<a class="ksc-carousel-more" title="Find out more..." href="' + item.link + '">'+theauthor+'</a>';
		}
		// add comment count if there are comments
		if (item.commentcount != '' && item.commentcount != '0' && String(item.commentcount) != 'undefined'){
			itemContent += '</div><a title="Read comments" href="'+item.comments+'" class="ksc-carousel-comments">'+item.commentcount+'</a>';
		}
		
		if ((colnum == 2 || colnum == 3) && (String(item.gg) != 'undefined' || (String(item.image) != 'undefined' && String(item.image) != 'null' ))){
			itemContent += "</div>";
		}
		return itemContent;
	}
	
	// Triming strings
	function trim(str, chars) {
		return ltrim(rtrim(str, chars), chars);
	}
	
	function ltrim(str, chars) {
		chars = chars || "\\s";
		return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
	}
	
	function rtrim(str, chars) {
		chars = chars || "\\s";
		return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
	}

};

/*
 * adapted from Stˇphane Roucheray infinate carousel
 * @extends jquery
 */


jQuery.fn.kiss_carousel = function(previous, next, noscroll){
	var sliderList = jQuery(this).children()[0];
	
	if (this && sliderList) {
		var increment = jQuery(sliderList).children().outerWidth("true"),
		elmnts = jQuery(sliderList).children(),
		numElmts = elmnts.length,
		sizeFirstElmnt = increment,
		shownInViewport = Math.round(jQuery(this).width() / sizeFirstElmnt),
		firstElementOnViewPort = 1,
		isAnimating = false;
		
		jQuery(sliderList).css('left', "0px");
		jQuery(sliderList).css('width',(numElmts+shownInViewport)*increment + "px");
		jQuery(previous).addClass('disabled');
		
		if (numElmts<= shownInViewport){
			jQuery(next).addClass('disabled');
			//isAnimating = true;
		} else {
			jQuery(next).removeClass('disabled');
			isAnimating = false;
		}
		

		
		// stop infinite scrolling
		if (numElmts > shownInViewport){		
			jQuery(previous).unbind("click").click(function(event){
				if (!isAnimating) {
				
					firstElementOnViewPort = firstElementOnViewPort - shownInViewport;
					
					if (firstElementOnViewPort <= 1){
						firstElementOnViewPort = 1;
						jQuery(previous).addClass('disabled');
					}
					jQuery(next).removeClass('disabled');
					
					var targetleft = ((firstElementOnViewPort-1) * increment) * -1;
					var now = parseFloat(jQuery(sliderList).css('left'));
					var diff = targetleft-now;
					
					jQuery(sliderList).animate({
						left: "+=" + diff,
						y: 0,
						queue: true
					}, "swing", function(){isAnimating = false;});
					isAnimating = true;
	
				}
				
			});
			
			jQuery(next).unbind("click").click(function(event){
				if (!isAnimating) {
				
					firstElementOnViewPort = firstElementOnViewPort + shownInViewport;
					
					if (firstElementOnViewPort >= (numElmts-(shownInViewport-1))){
						firstElementOnViewPort = numElmts-(shownInViewport-1);
						jQuery(next).addClass('disabled');
					}
					
					if (firstElementOnViewPort > 1){
						jQuery(previous).removeClass('disabled');
					}
					
					var targetleft = ((firstElementOnViewPort-1) * increment) * -1;
					var now = parseFloat(jQuery(sliderList).css('left'));
					var diff = now - targetleft;
					
					
					
					jQuery(sliderList).animate({
						left: "-=" + diff,
						y: 0,
						queue: true
					}, "swing", function(){isAnimating = false;});
					isAnimating = true;
				}
			});
		} else {
			jQuery(previous).addClass('disabled');
			jQuery(next).addClass('disabled');
		}
		
	}
};

