var YS = {}

// Init on DOM ready
$(function() {
	YS.Basket.init();
	YS.Menu.init();
	YS.Articles.init();
	YS.Hitlist.init();
	YS.Artist.init();
	YS.Tracklist.init();
	YS.Top100.init();
	YS.Ringtones.init();
	YS.Gallery.init();
	YS.Player.init();
});

YS.setup = {
	imagepath: rootdir + 'img/yousee/',
	imagedesign: rootdir + 'img/yousee/design/'
}

YS.texts = {
	NextPage: 'Nęste side',
	PrevPage: 'Forrige side',
	GotoPage: 'Side',
	PrevGallery: 'Forrige',
	NextGallery: 'Nęste',
	CloseGallery: 'Luk vindue',
	VolumeText: 'Volume: *percent*%' 
}

YS.Menu = {
	
	MenuId: 'ul#k_leftnav',
	
	init: function() {
		$(this.MenuId + ' li').prepend('<span class="clickfield">&nbsp;</span>');
		$(this.MenuId + ' span.clickfield').click(function() { YS.Menu.fold(this,'button'); return false; });
		$(this.MenuId + ' li ul li ul li span.clickfield').remove();
		$(this.MenuId + ' li[class!="k_current"] ul').css('display','none');
	},
	
	fold: function(el,type) {
		if (type == undefined) {
			$(el).parent().toggleClass('k_current');
			$(el).next().slideToggle('slow');
		} else {
			$(el).parent().toggleClass('k_current');
			$(el).next().next().slideToggle('slow');
		}
	}
}

YS.Player = {
	
	/* Volume stuff */
	volumeDiv: 'div#player div.volumebar div#volume',
	volumeImg: 'div#player div.volumebar table td',
	volumeMaxHeight: null,
	normalVolume: 50,
	useVolumeCookie: false,
	cookieName: 'yousee_volume',

	init: function() {
		
		if(this.useVolumeCookie===true) {
			name = $.cookie(this.cookieName);
			if(name) this.normalVolume = name;
			else $.cookie(this.cookieName,this.normalVolume);
		}
		
		this.volumeMaxHeight = $(this.volumeDiv).height();
		
		$(this.volumeImg).click(function(e) {
			var yPos = (e.layerY ? e.layerY : e.offsetY);
			
			yPos = Math.max(yPos, 17);
			yPos = Math.min(yPos, 106);
			yPos -= 17;
			
			percent = Math.round(YS.Player.volumeMaxHeight - (YS.Player.volumeMaxHeight * (yPos/100)));
			YS.Player.setVolume(percent);
			return false;
		});
		
		this.setVolume(this.normalVolume);
	},
	
	setVolume: function(pixels) {
		if(this.useVolumeCookie===true) $.cookie(this.cookieName,pixels);
		percent = Math.round(pixels*100 / this.volumeMaxHeight);
		$(this.volumeDiv).height(pixels).parent().parent().attr('title',YS.texts.VolumeText.replace('*percent*',percent));
		PlayerControls.setVolume(percent); // Call TDCs working PLAY player to adjust volume of audio
	},
	
	/* Folding elements */
	foldId: 'div#folding',
	foldImg: 'div#player_foldup img',

    foldDown: function() {
        $(this.foldId).slideToggle('fast');
        $(this.foldImg).attr('src',YS.setup.imagepath+'/player/foldup.gif');
    },

    foldUp: function() {
        $(this.foldId).slideToggle('fast');
        $(this.foldImg).attr('src',YS.setup.imagepath+'/player/folddown.gif');
    },
	
	fold: function() {
		if($(this.foldId).css('display') == 'block') {
            this.foldUp();
		} else {
		    this.foldDown();
		}
	},
	
	/* Play buttons */
	play: false,
	
	toggleplay: function() {
		(this.play ? this.stopplay() : this.startplay());
	},
	
	stopplay: function() {
		
	},
	
	startplay: function() {
		
	}
}

YS.Basket = {
	
	TrackId: 'span#basket_tracks',
	AlbumId: 'span#basket_albums',
	PriceId: 'span#basket_price',
	Itemcount: 'span#basket_itemcount',
	
	FoldId: 'div#basket',
	
	init: function() {
		$(this.FoldId + ' dt')
		.click(function() { $(this).toggleClass('current').next().slideToggle('slow'); })
		.not('.current')
		.next()
		.css('display','none')
	},
	
	addItem: function(lmid, wmencpid, slid, uotid, flag) {
		/*
		 * Ajax - add ID to database
		 * @example ajax
		 * $.post('addtobasket.php', {
		 *	data: {item: id},
		 *	error: function(xml) {
		 *		alert(xml); // Just alerting the output if an error accurs
		 *	},
		 *	success: function() {
		 *		if(type == track) {
		 *          count...... and so on
		 *       } else {
		 *         ....
		 *       }
		 *       this.setTotalItems(1);
		 *       this.setTotalPrice(price);
		 *       return true; // REMEMBER THIS LINE!
		 *	}
		 * })
		 *
		 * @example addtobasket.php
		 * <?php
		 * $basket = new Basket();
		 * echo $basket->addItem($_POST['item']);
		 * ?>
		 */
		/*
	    if (flag == 0) {
	        var params  = lmid + ':' + slid + ':' + wmencpid + ':' + wmencpid + ':' + uotid + ':' + uotid + ':' + flag;
	        var fullurl = url + 'viewcart';
	    } else {
	        var params  = lmid + ':0:' + wmencpid + ':' + wmencpid + ':' + uotid + ':' + uotid + ':' + flag;
	        var fullurl = url + 'updatecart_ext';
	    }

	    $.ajax({
	        type: "POST",
	        dataType: "html",
	        url: fullurl,
	        data: "UpdateCart=" + params,
	        async: true,
	        success: function(result) {
	           $("#k_leftnav .t_basket table.total").html(result);
	        }
	    });
	    */
	    updatecart_ext(lmid, wmencpid, slid, uotid, flag);
	},
	
	removeItem: function(price,type) {
		this.setTotalItems(-1);
		this.setTotalPrice(-price);
	},
	
	setTotalPrice: function(price) {
		count = parseInt($(this.PriceId).html());
		$(this.PriceId).html(count+price);		
	},
	
	setTotalItems: function(i) {
		count = parseInt($(this.Itemcount).html());
		$(this.Itemcount).html(count+i);
	}
}

YS.Articles = {
	
	Total: 0,
	Id: 'div#articles',
	Pager: 'p.articlepager',
	current: null,
	$prev: null,
	$next: null,
	
	init: function() {
		this.current = 0;
		this.Total = count = $(this.Id + ' li').size();
		if(this.Total<1) return ;
		
		$(this.Id + ' li:not(:first)').hide();
		this.addLinks(null);

		// Add previous button
		this.$prev = $('<div class="prev"><a class="articles clickable disabled">' + YS.texts.PrevPage + '</a></div>')
		.prependTo(this.Id)
		.click(function() {
			if($(this).find("a.articles").is(".disabled")) return false;
			YS.Articles.show(YS.Articles.current - 1);			
		});
		
		// Add next button
		this.$next = $('<div class="next"><a class="articles clickable">' + YS.texts.NextPage + '</a></div>')
		.prependTo(this.Id)
		.click(function() {
			if($(this).find("a.articles").is(".disabled")) return false;
			YS.Articles.show(YS.Articles.current + 1);
		});
	},
	
	addLinks: function(id) {
		if(id !== null) {
			$(this.Id+' '+this.Pager+' a img').attr('src',YS.setup.imagedesign + 'pagerdot.gif');
			$(this.Id+' '+this.Pager+' a#article_switch_'+id+' img').attr('src',YS.setup.imagedesign + 'pagerdot_current.gif');
		} else {
			for(index=0;index<this.Total;index++) {
				$(this.Id+' '+this.Pager).append('<a id="article_switch_' + index + '" class="pointer" onclick="YS.Articles.show(' + index + ')"><img class="noborder" src="' + YS.setup.imagedesign + (index==this.current ? 'pagerdot_current.gif' : 'pagerdot.gif') + '" alt="' + YS.texts.GotoPage + ' ' + (index+1) + '" title="' + YS.texts.GotoPage + ' ' + (index+1) + '"></a>');
			}	
		}
	},
	
	show: function(id) {
		$(this.Id + ' li:eq('+this.current+')').hide();
		$(this.Id + ' li:eq('+id+')').show();
		this.current = id;
		
		this.addLinks(id);
		
		// enable/disable previous button
		(id > 0) ? this.$prev.find("a.articles").removeClass("disabled") : this.$prev.find("a.articles").addClass("disabled");
		// enable/disable next button
		(id == this.Total - 1) ? this.$next.find("a.articles").addClass("disabled") : this.$next.find("a.articles").removeClass("disabled");	
	}
}

YS.Hitlist = {
	
	Total: 0,
	Id: 'div#hitlist',
	
	init: function() {
		
		$(this.Id+' ul li:first-child').css('border-top','none');
		
		this.Total = $(this.Id+' div.k_block').size();
		
		switch(this.Total) {
			case 0:
				$(this.Id).remove();
				return ;
			case 1:
				$(this.Id+' div.k_block').addClass('k_block_double').addClass('t_block_double');
				break;
			case 2:
				$(this.Id + ' div.k_block').addClass('k_block_single').addClass('floatleft');
				$(this.Id + ' div.k_block:eq(1)').addClass('t_block_floatedright');
				break;
			default:
				$(this.Id + ' div.k_block').addClass('t_block_single');
				$(this.Id + ' div.k_block:gt(2)').hide();
				$(this.Id + ' div.k_block:eq(0)').addClass('t_3block_left').addClass('t_3block_left_margintop').show();
				
				$(this.Id + ' div.k_block:eq(0) li:first').css('margin-top',12);
				$(this.Id + ' div.k_block:eq(0) p.more').css('margin-bottom',-5);
				/* IE < 7.0 Fixes */
				if($.browser.msie === true && $.browser.version < 7) {
					$(this.Id + ' div.k_block:eq(0) li:first').css('margin-top',22);
					$(this.Id + ' div.k_block:eq(0) p.more input').css('margin-top',5);
					$(this.Id + ' div.k_block:eq(0) p.more').css('margin-bottom',-5);
				}
				/* end IE fix */
				
				/* IE < 7.0 Fixes */
				if($.browser.msie === true && $.browser.version == 7) {
					$(this.Id + ' div.k_block:eq(0) li:first').css('margin-top',22);
					$(this.Id + ' div.k_block:eq(0) p.more input').css('margin-top',5);
					$(this.Id + ' div.k_block:eq(0) p.more').css('margin-bottom',-4);
				}
				/* end IE fix */
				
				$(this.Id + ' div.k_block:eq(1)').addClass('t_3block_middle').show();
				$(this.Id + ' div.k_block:eq(2)').addClass('t_3block_right').show();
				if (this.Total > 3) this.addnextprev(0,2);
				break;
		}
	},
	
	addnextprev: function(prev,next) {
		// Add previous button
		$('<div class="prev hitlistnextprev"><a class="hitlist clickable '+(prev>0 ? '' : 'disabled')+'">' + YS.texts.PrevPage + '</a></div>').prependTo(this.Id+' div.hitlistitem:eq('+prev+')').click(function(){
			if ($(this).find("a.hitlist").is(".disabled")) return false;
			YS.Hitlist.show(prev - 1);
		});
		
		// Add next button
		$('<div class="next hitlistnextprev"><a class="hitlist clickable '+(this.Total>(next+1) ? '' : 'disabled')+'">' + YS.texts.NextPage + '</a></div>').prependTo(this.Id+' div.hitlistitem:eq('+next+')').click(function(){
			if ($(this).find("a.hitlist").is(".disabled")) return false;
			YS.Hitlist.show(next + 1);
		});
	},
	
	show: function(index) {

		/* IE < 7.0 Fixes */
		if($.browser.msie === true && $.browser.version < 7) {
			//$(this.Id).css('margin-top',-33);
		}
		/* end IE Fixes */
	
		$(this.Id + ' div.k_block').removeClass('t_3block_left').removeClass('t_3block_middle').removeClass('t_3block_right').hide();
		
		$(this.Id + ' div.hitlistnextprev').remove();
		
		if(index<3) {
			this.init();
		} else {
			$(this.Id + ' div.k_block').eq(index-2).addClass('t_3block_left').show();
			$(this.Id + ' div.k_block').eq(index-1).addClass('t_3block_middle').show();
			$(this.Id + ' div.k_block').eq(index).addClass('t_3block_right').show();
			this.addnextprev(index-2,index);
		}
	}
}


YS.Artist = {
	Id: 'div#multilist dl',
	DownloadId: 'div#artist div.buyinfo',
	
	PriceId: ' div.buy span.realprice',
	Price: null,
	TypeId: ' div.buy span.type',
	Type: null,
	BuyId: ' div.buy span.id',
	Buy: null,
	
	init: function() {
		/* Get Buy types */
		this.Price = parseInt($(this.DownloadId + this.PriceId).css('display','none').html());
		this.Type = parseInt($(this.DownloadId + this.TypeId).css('display','none').html());
		this.Buy = parseInt($(this.DownloadId + this.BuyId).css('display','none').html());
		
		/* Buy icon */
		$(this.DownloadId +' div.buy').click(function() { YS.Artist.showAdding(YS.Artist.Type,YS.Artist.Price,YS.Artist.Buy); })
		
		/* Multimedia list folding */
		$(this.Id + ' dt')
		.click(function() {
			if($.browser.msie === true && $.browser.version < 7) {
                $('div#multilist').css('padding-right',0);
                $('div#multilist').css('margin-right',0);
			}
			YS.Artist.foldAll();
            $(this).toggleClass('current').next().slideToggle('slow');
		})
		.not('.current')
		.next()
		.css('display','none')
	},
	
	foldAll: function() {
	    $("div#multilist dl dt.current").removeClass('current').next().slideToggle('fast');
	},

	showAdding: function(type,price,id) {
		if (type !== null && price !== null && id !== null) {
			//YS.Basket.addItem(price, type, id);
			$(this.DownloadId + '  div.adding').fadeIn('slow', function() {
				setTimeout("YS.Artist.removeAdding()",1000);
			});
		}
	},
	
	removeAdding: function() {
		$(this.DownloadId + '  div.adding').fadeOut('slow');
	}
}

YS.Tracklist = {
	Id: 'div#tracklist table',
	
	init: function() {
		/* On mouse over functions */
		$(this.Id+' tr').mouseover(function() {
			$(this).addClass('over');
		}).mouseout(function() {
			$(this).removeClass('over');
		});
		
		/* Sort table method */
		$(this.Id).tablesorter({
			headers: { 0: { sorter: false}, 4: {sorter: false} } 
		});
	}
}

YS.Top100 = {
	
	Id: 'div.top100',
	
	init: function() {
		/* Remove padding from last line*/
		size = $(this.Id+' li').size();
		$(this.Id+' li:gt('+(size-3)+')').css('margin-bottom',-30);
	}
}

YS.Ringtones = {
	Id: 'div#ringtones',
	
	init: function() {
		/* On mouse over functions */
		$(this.Id+' tr').mouseover(function() {
			$(this).addClass('over');
		}).mouseout(function() {
			$(this).removeClass('over');
		});
	}
}

// Billedefremviser
YS.Gallery = {
	
	Id: 'div#gallery div.list',
	current: null,
	listlength: null,
	popup: null,
	
	init: function() {
		$(this.Id).css('display','none');
		
		$(this.Id + ' img.close').click(function() {
			this.popup.close();
			return false;
		});
		
		this.listlength = $(this.Id).length;
		this.show(0);
	},
	
	openwindow: function(url) {
		this.popup = window.open(url);
	},
	
	addprevnext: function() {
		/* Resetting all next/prev */
		$(this.Id).children('.image').children('.choose').children('.prev').css('display','none').click(function() { return false; });
		$(this.Id).children('.image').children('.choose').children('.next').css('display','none').click(function() { return false; });

		/* Prev */
		if (this.current > 0) {
			$(this.Id).eq(this.current).children('.image').children('.choose').children('.prev').css('display', 'block').click(function(){
				YS.Gallery.show(YS.Gallery.current - 1);
				return false;
			})
		}
		
		/* Next */
		if(this.current < this.listlength-1) {
			$(this.Id).eq(this.current).children('.image').children('.choose').children('.next').css('display','block').click(function() {
				YS.Gallery.show(YS.Gallery.current+1);
				return false;
			})
		}
	},
	
	show: function(number) {
		this.current = number;
		$(this.Id).css('display','none');
		$(this.Id).eq(this.current).css('display','block');
		this.addprevnext();
	}
}
