/*
eoPlayer version: 1.0.1
eoplayer.js
popupParser.js

以下のライブラリに依存してます。
jquery.js
jquery.cookie.js
*/

var popupParser = function(){
	//絶対パスとかを書いてもらうと助かります。
	var playerDir = '/eohikari-ch/player/';
	
	//ポップアップするrel
	var rel = "eoPlayer";
	
	var px = 0;
	var py = 0;
	
	//playerのURL
	var P2M = playerDir + "2m.html";
	var P500K = playerDir + "500k.html";
	
	//プレイヤーのサイズ
	var size = "2m";
	
	//ボリューム
	var volume = 80;
	
	var his = [];
	
	var ua = navigator.userAgent.toLowerCase();
	
	var client = {
		ie    :ua.indexOf('msie') > -1,
		ie6   :ua.indexOf('msie 6') > -1,
		ie7   :ua.indexOf('msie 7') > -1,
		gecko :ua.indexOf('gecko') > -1 && ua.indexOf('safari') == -1,
		webkit:ua.indexOf('applewebkit/') > -1,
		win   :ua.indexOf('windows') > -1 || ua.indexOf('win32') > -1,
		mac   :ua.indexOf('macintosh') > -1 || ua.indexOf('mac os x') > -1,
		linux :ua.indexOf('linux') > -1
	};
	
	PP = {
		
		init : function(url) {
			
			setCookie("list", url);
			
			if (!getCookie("size")) {
				setCookie("size", size);
			} else {
				size = getCookie("size");
			}
			
			if (getCookie("his")) {
				his = getCookie("his").split("$$");
			}
			
			if (size == "500k") relParser("500k");
			
			$("a[rel='" + rel + "']").bind("click", onclick);
			
			$(document).mousemove(function(e){px=e.pageX;py=e.pageY;})
			.mouseup(revmoveSwich);
		},
		
		set500k : function() {
			relParser("500k");
		},
		
		set2m : function() {
			relParser("2m");
		},
		
		openEoPlayer : function(t, u) {
			setCookie("title", t);
			setCookie("url", u);
			
			if ( client.ie && client.win ) {
				openEoPlayer(t, u);
				setHis(t, u);
			} else {
				openSwich(t, u);
			}
		},
		
		size : function() {
			return size;
		}
		
	};
	
	function revmoveSwich() {
		$("#size_selecter").remove();
	}
	
	function relParser(s) {
		
		size = s;
		
		$("a[rel='" + rel + "']").each(function(i) {
			var h = $(this).attr("href");
			
			if (s == "500k") {
				h = h.replace("_2m.asx", "_500k.asx");
			} else {
				h = h.replace("_500k.asx", "_2m.asx");
			}
			
			$(this).attr("href", h);
		});
	}
	
	function openEoPlayer(t, u){
		if (size == "2m") {
			window.open(P2M, rel, 'width=880, height=585, menubar=no, toolbar=no, scrollbars=no');
		} else {
			window.open(P500K, rel, 'width=557, height=342, menubar=no, toolbar=no, scrollbars=no');
		}
	}
	
	function onclick(e) {
		var t = e.currentTarget.title;
		var u = e.currentTarget.href;
		
		PP.openEoPlayer(t, u);
		
		return false;
	}
	
	function setCookie(n, v) {
		$.cookie(rel + "_" + n, v, {expires:365, path:'/'});
	}
	
	function getCookie(n) {
		return $.cookie(rel + "_" + n);
	}
	
	function openSwich(t, u) {
		var sx = px + "px";
		var sy = (py - 25) + "px";
		var h = '<div id="size_selecter"><div id="size_selecter_base"></div>';
		h += '<div id="size_selecter_2m"></div>';
		h += '<div id="size_selecter_500k"></div>';
		h += '</div>';
		
		$("body").append(h);
		$("#size_selecter").css({
			width:"83px",
			height:"54px",
			position:"absolute",
			top:sy,
			left:sx,
			zIndex:1000000
		});
		$("#size_selecter_base").css({
			width:"83px",
			height:"54px",
			backgroundImage:"url(" + playerDir + "/img/size_selecter_base.png)",
			position:"absolute",
			top:"0px",
			left:"0px"
		});
		$("#size_selecter_2m").css({
			width:"83px",
			height:"54px",
			backgroundImage:"url(" + playerDir + "/img/size_selecter_2m.gif)",
			position:"absolute",
			top:"2px",
			left:"7px",
			width:"70px",
			height:"23px",
			cursor:"pointer",
			backgroundPosition:"top left"
		})
		.mouseover(mouseover).mouseout(mouseout).mousedown(function(){
			size = "2m";
			openWMP();
		});
		$("#size_selecter_500k").css({
			width:"83px",
			height:"54px",
			backgroundImage:"url(" + playerDir + "/img/size_selecter_500k.gif)",
			position:"absolute",
			top:"25px",
			left:"7px",
			width:"70px",
			height:"23px",
			cursor:"pointer",
			backgroundPosition:"top left"
		})
		.mouseover(mouseover).mouseout(mouseout).mousedown(function(){
			size = "500k";
			openWMP();
		});
	}
	
	function openWMP(){
		var h = getCookie("url");
		var t = getCookie("title");
		
		if (size == "500k") {
			h = h.replace("_2m.asx", "_500k.asx");
		} else {
			h = h.replace("_500k.asx", "_2m.asx");
		}
		
		setHis(t, h);
		window.open(h);
	}
	
	//ボタンのロールオーバー
	function mouseover(e){
		$(this).css("backgroundPosition", "bottom");
	}
	
	function mouseout(e){
		$(this).css("backgroundPosition", "top");
	}
	
	function setHis(t, u){
		var l = location.href;
		var dd = new Date();
		var yy = dd.getYear();
		var mm = dd.getMonth() + 1;
		
		var hh = dd.getHours();
		var nn = dd.getMinutes();
		
		var dd = dd.getDate();
		if (yy < 2000) { yy += 1900; }
		var set = t + "," + l + "," + u + "," + yy + "年" + mm + "月" + dd + "日 " + hh + ":" + nn;
		
		var b = his[0];
		if (b) {
			b = b.split(",");
			
			if (t == b[0]) {
				his[0] = set;
			} else {
				his.unshift(set);
			}
			
			if (his.length > 4) his.pop();
		} else {
			his[0] = set;
		}
		
		var s = "";
		for (i in his){
			s += his[i];
			if (i < his.length - 1) s += "$$";
		}
		setCookie("his", s);
	}
	
	return PP;
}();


//console.log("deta?")
