// JavaScript Document

eoch.ch = function(){
	
	var data = null;
	var intervalID = 0;
	var slideIID = 0;
	var listAc = 0;
	var listTa = 0;
	var listScrollHeight = 49 * 4;
	var news = [];
	
	E = {
		init : function(xmlurl){
			$.ajax({
				url : xmlurl,
				dataType : "xml",
				timeout : 1000*10,
				success : loadXmlSuccess
			});
			
			$.ajax({
				url : "../common/xml/present.xml",
				dataType : "xml",
				timeout : 1000*10,
				success : function(xml){$("#side_div").append($(xml).find("present").text());}
			});
		},
		
		listOver : function(i){
			if (listAc == i) return;
			
			var b = $("#trailer_list div").eq(listAc);
			var n = $("#trailer_list div").eq(i);
			var img = data.find("photo").eq(i).text();
			
			b.removeClass("on").addClass("off");
			n.removeClass("off").addClass("on");
			
			$("#img_s").attr("src", img);
			
			listAc = i;
		},
		
		listAutoNext : function(){
			var i = listAc;
			
			if ($("#trailer_list div").length == i + 1) {
				i = 0;
				scrollTop(0);
			} else {
				++i;
			}
			
			if (i%3 == 0 && i != 0) {
				listDown(null);
			}
			
			this.listOver(i);
		},
		
		listOpen : function(i){
			/*if (!popupParser) return;
			var x = data.find("movie").eq(i);
			var t = x.find("title").text();
			var u = data.find("playlist").attr("asxpath") + x.find("asx").text() + "_" + popupParser.size() + ".asx";
			popupParser.openEoPlayer(t, u);*/
			
			var url = 'http://bcove.me/' + data.find("movie").eq(i).find("bcove").text();
			//window.open(url, 'player', 'width=600, height=415, menubar=no, toolbar=no, scrollbars=no');
			$.colorbox({
				//width:"960",
				innerWidth:"960",
				//height:"445",
				innerHeight:"445",
				href:url,
				scrolling:false,
				iframe:true
			});
		},
		
		swch : function(o){
			var p = $(o).parent();
			
			if(p.hasClass("close")){
				p.removeClass("close");
			} else {
				p.addClass("close");
			}
		}
	};
	
	function loadXmlSuccess(xml){
		data = $(xml);
		
		asxPath = data.find("playlist").attr("asxpath");
		var ch = data.find("playlist").attr("id");
		
		var tre = '';
		var mo = '';
		data.find("movie").each(function(i){
			var th = $(this);
			var date = th.attr("update");
			var pre = th.attr("present");
			var cut = th.attr("cut");
			var t = th.find("title").text();
			var sub = th.find("subtitle").text();
			var desc = th.find("desc").text();
			var img = th.find("photo").text();
			var thum = th.find("thum").text();
			var bcove = th.find("bcove").text();
			var more = th.find("more").text();
			
			/*----------------*/
			
			if (i == 0) {
				$("#img_s").attr("src", img).css({zIndex:100});
				
				tre += '<div id="new_movie_detail">';
				tre += '<div id="new_movie_bar" class="c_' + ch + '"></div>';
				tre += '<h3>' + t + '</h3>';
				tre += '<div class="p">' + desc + '</div><span>' + date + 'XV</span></div>';
				tre += '<div id="new_movie_ctrl">';
				if (pre) tre += '<a id="bt_pre" href="../present/"></a>';
				tre += '<a id="bt_play" href="javascript:eoch.ch.listOpen(' + i + ');" rel="eoPlayer" title="' + t + '"></a>';
				tre += '</div>';
				
				if (more) $("#new_movie").after('<div id="new_movie_more">'+more+'</div>');
				
			} else {
				if (date) {
					mo += '<div class="mov_li"><div class="ch_color c_' + ch + '"></div>';
					mo += '<img class="mov_li_img" src="' + thum + '" alt="' + t + '" width="110" height="83" />';
					mo += '<h3>' + t + '</h3><p>' + desc + '</p>';
					if (more) mo += more;
					mo += '<div class="mov_li_footer"><span>' + date + 'XV</span>';
					mo += '<a class="mov_play" href="javascript:eoch.ch.listOpen(' + i + ');" rel="eoPlayer" title="' + t + '"></a>';
					if (pre) mo += '<a href="../present/" class="mov_present"></a>';
					mo += '</div></div>';
				}
			}
		});
		
		$("#new_movie").append(tre);
		$("#move_list").append(mo);
	}
	
	function listUp(e){
		scrollTop(-listScrollHeight);
	}
	
	function listDown(e){
		scrollTop(listScrollHeight);
	}
	
	function scrollTop(s){
		var wh = $("#trailer_list").height();
		var sh = $("#trailer_list").attr("scrollHeight") - wh;
		var se = listTa + s;
		
		if (se > sh) {
			se = sh;
		} else if (se < 0) {
			se = 0;
		}
		
		if (s == 0) se = 0;
		
		E.listOver(Math.floor(se/49));
		
		listTa = se;
		$("#trailer_list").stop()
		.animate({scrollTop:se}, {duration:300, easing:"easeOutCubic"});
	}
	
	function nextTpics(e){
		$("#topics_list").append($("#topics_list li:first"));
	}
	
	return E;
}();

