// $Id: mxProtoType.js, v 0.0.1 Wed Mar 19 15:41:16 MSK 2008
//-------------------------------------------------------//
// Author: Mangushev M.                                  //
// Website: http://www.2mx.ru/                           //
// License Type: (c)Copyright by Author                  //
//-------------------------------------------------------//



function getXmlHttp() {
    var xmlhttp;
    try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (E) {
            xmlhttp = false;
        }
    }
    if (!xmlhttp && typeof XMLHttpRequest != '0') {
        xmlhttp = new XMLHttpRequest();
    }
    return xmlhttp;
}

//-------------------------------------------------------//
//--- YouTube function's --------------------------------//
	function randomRequest(tag) {
		var uReq = new Array(
				"италия",
				"итальянская мода",
				"италия фильм",
				"италия отдых",
				"итальянская",
				"итальянец",
				"итальянский",
				"италия рим",
				"италия венеция",
				"италия туризм",
				"италия дизайн",
				"ardo",
				"italy",
				"италия город",
				"италия природа"
			);
		// Техника ARDO
		var tag1 = new Array(
				"reklama ardo",
				"ardo"
			);
		// Италия для всех
		var tag2 = new Array(
				"италия",
				"италия для всех",
				"italy for all"
			);
		// Итальянская мода
		var tag3 = new Array(
				"италия",
				"итальянская мода",
				"italy fashion"
			);
		// Кухня Италии
		var tag4 = new Array(
				"италия",
				"italy kitchen",
				"italy food"
			);
		// Итальянское вино
		var tag5 = new Array(
				"италия",
				"italy vine",
				"wine италии"
			);
		// Италия. Туризм
		var tag6 = new Array(
				"италия",
				"италия туризм",
				"turism italy"
			);
		// Итальянский дизайн
		var tag7 = new Array(
				"италия",
				"italy design",
				"дизайн италия"
			);
		// Флирт и знакомства
		var tag8 = new Array(
				"италия",
				"италия для всех",
				"italy for all"
			);
		// Итальянское кино
		var tag9 = new Array(
				"италия",
				"cinema italy",
				"kino italy"
			);
		// Музыкальная Италия
		var tag10 = new Array(
				"италия",
				"music italy",
				"италия музыка"
			);
		// Итальянский драйв
		var tag11 = new Array(
				"италия",
				"italy drive",
				"italy style"
			);
		// Юмор
		var tag12 = new Array(
				"италия",
				"italy humor",
				"italy joke"
			);
		
		if (tag) {
			if (tag == 'tag-1') uReq = tag1;
			if (tag == 'tag-2') uReq = tag2;
			if (tag == 'tag-3') uReq = tag3;
			if (tag == 'tag-4') uReq = tag4;
			if (tag == 'tag-5') uReq = tag5;
			if (tag == 'tag-6') uReq = tag6;
			if (tag == 'tag-7') uReq = tag7;
			if (tag == 'tag-8') uReq = tag8;
			if (tag == 'tag-9') uReq = tag9;
			if (tag == 'tag-10') uReq = tag10;
			if (tag == 'tag-11') uReq = tag11;
			if (tag == 'tag-12') uReq = tag12;
		}
		uReq = randSortArray(uReq);
		
//		alert(uReq.join("\n"));
		return encodeURIComponent(uReq[0]);
	}
	function randSortArray(arr) {
		arr.sort(function() {return 0.5 - Math.random()})
		return arr;
	}
	function searchSales(url) {
		var pars = '';
		var myAjax = new Ajax.Request('/xml/proxy.php?url=' + encodeURIComponent(url), {
					method: 'get',
					parameters: pars,
					onComplete: showResponse
				}
			);
	
	}
	
	function show_props(obj, obj_name) {
		var result = ""
		for (var i in obj)
			result += obj_name + "." + i + " = " + obj[i] + "\n"
		return result
	}
	function crVideoBl(url,title,description){
		var text = "<div>";
		var id = url.split("watch?v=");
		id = id[1];
		text += "<a onmouseout=\"hidehint(this,event)\" onmousemove=\"showhint(this,event)\" href=\"/js/player.php?id="+id+"\">";
//		text += "<a onmouseout=\"hidehint(this,event)\" onmousemove=\"showhint(this,event)\" href=\"#youPlayer\" onClick=\"getVideo('youPlayer','ytapiplayer','"+id+"'); return false;\">";
		text += "<img src=\"http://img.youtube.com/vi/"+id+"/2.jpg\" width=\"100\" border=\"0\" />";
		text += "<span><h4>"+title+"</h4>"+description+"</span></a>";
		text += "</div>";
		return text;
	}
	
	function showResponse(originalRequest){
	//put returned XML in the textarea
		//$('result').value = originalRequest.responseText;
		var	elem = originalRequest.responseXML.documentElement;
		var xml = XMLParser.parse(elem);
		var content = '';
		//$('show_props').innerHTML = show_props(elem,"root");
		res = xml.path('rss->channel->item');
		res = randSortArray(res);
		//for (i=0;i<res.length;i++) {//res.length
		for (i=0;i<res.length&&i<6;i++) {//res.length
			content += crVideoBl(res[i].$link.value,res[i].$title.value,res[i].$description.value);
		}
		$('allVideo').innerHTML = content;
	}
	//
	function getVideo(obj_div,obj_id,video_id) {
		showBlock(obj_div);
		$(ytapiplayer).innerHTML = "";
		var params = { allowScriptAccess: "always" };
		var atts = { id: "myytplayer" };
		swfobject.embedSWF("http://www.youtube.com/v/"+video_id+"&fmt=6&enablejsapi=1&playerapiid=ytplayer&color1=0xffffff&color2=0x054BCD&autoplay=1&border=0", 
					   obj_id, "425", "356", "8", null, null, params, atts);
	}