// init jquery scripts (document.load)
var data;
$(document).ready(function(){

	$('.fade').innerfade({
		speed: 1000,
		timeout: 6000,
		type: 'random_start',
		containerheight: '1.5em'
	});

	$('.tTip').betterTooltip({speed: 150, delay: 300});

	window.fbAsyncInit = function() {
		FB.init({appId: '136834723005509', status: true, cookie: true,xfbml: true});
	};
	var fbroot = document.createElement("div");
	fbroot.id = "fb-root";
	var e = document.createElement('script'); e.async = true;
	e.src = document.location.protocol +'//connect.facebook.net/en_US/all.js';
	fbroot.appendChild(e);
	$("body").append(fbroot);


	//$.jsonP(Proxies.madradio.homepage, function(resp) {
	$.getJSON(Proxies.madradio.homepage, function(resp) {
		data = resp;
		//try {
			Render();
		//}catch(e){
		//	alert("There Was a problem occured please refresh "+e);
		//}
	});

	loadCSSInclude("/includes/css/madradio.css");
	$(".madlogo").find("a").attr("href","/madradio/");
});



function Render(){

	$("#content").html("").css({width:"1100px","overflow":"hidden"});
	var hotbox = $("#template_box_single").clone(true);
	var parou = $("#template_quad_box_images").clone(true);
	var ekpompes = $("#template_quad_box_images").clone(true);
	var contest = $("#template_box_quad").clone(true);
	var newsrotation = $("#template_box_select").clone(true);

	var livenow = $("#template_box_single_container").clone(true);
//	var livenow = $("#template_box_agenda").clone(true);

	var exitpoll = $("#template_box_list_chart").clone(true);
	var mappes = $("#template_box_single").clone(true);
	var poll = $("#template_box_poll").clone(true);
	var special = $("#template_quad_box_images").clone(true);
	//var live = $("#template_box_quad").clone(true);
	//var interviews = $("#template_box_single").clone(true);
	//var video = $("#template_box_single_container").clone(true);
	var twitter = $("#template_box_single_container").clone(true);


	$("#content").append(newsrotation);
	$("#content").append('<div class="boxespace8 left"></div>');
	$("#content").append(exitpoll);
	$("#content").append('<div class="boxespace9 left"></div>');
	$("#content").append(livenow);

	newline();

	//LINE 2
	$(".boxe_a",ekpompes).attr("style","");
	$("#content").append(ekpompes);
	$("#content").append('<div class="boxespace8 left"></div>');
	$(".boxe_a",parou).attr("style","");
	$("#content").append(parou);
	$("#content").append('<div class="boxespace9 left"></div>');
	displayAdBox($("#content"));

	newline();

	//LINE 3
	$("#content").append(special);
	$("#content").append('<div class="boxespace8 left"></div>');
	$("#content").append(twitter);
	$("#content").append('<div class="boxespace9 left"></div>');
	displayAdBox($("#content"));

	newline();

	//LINE 4
	//$("#content").append(live.show());
	//$("#content").append(interviews.show());
	//$("#content").append(video);
	$("#content").append(mappes.show());
    $("#content").append('<div class="boxespace8 left"></div>');
   	//var promoBox = get("template_simplecategory",$("#content"));
	$("#content").append(contest);
	$("#content").append('<div class="boxespace9 left"></div>');
	displayFacebook($("#content"), '<fb:like-box profile_id="122765171298" width="302" height="312" stream="false" header="false"></fb:like-box>',true);

	newline();

	hotbox.singleBox({
		title: data.hot[0].title,
		id:data.hot[0].id,
		summary: data.hot[0].summary,
		header: "ειδήσεις",
		header_href:"news.php",
		href:data.hot[0].href,
		image: convertImage(data.hot[0].photobig,"big"),
		original_image:site+"/"+data.hot[0].photobig
	});
    hotbox.show();

	parou.quadbox({
		header:"παραγωγοί",
		header_href:"producers.php",
	  	data:data.parous[0],
	  	hovver_effect:true
    });
    parou.show();

	ekpompes.quadbox({
         header:"εκπομπές",
		 header_href:"shows.php",
		 data:data.ekpompes[0],
		 hovver_effect:false
    });
    ekpompes.show();

	$.each(data.contest,function(i,n){n.caption = n.title;n.image = convertImage(n.photo,"org");});
	contest.splitBox({
		header: "διαγωνισμοί",
		header_href:'/madradio/contests.php',
		data: data.contest
	});
	contest.show();

	newsrotation.selectBox({
		header: 'what’s on',
		header_href: 'news.php',
		startpointer:0,
		size:6,
		data: data.hot,
		effect_hover:true
	});
	newsrotation.show();
	newsrotation.find('img').css({'width':'352'});
	
	/*interviews.singleBox({
		title:"",
		header: "interviews",
		header_href: "http://www.mad.tv/madradio/interviews/",
		image: "/listen/interviews.jpg",
		image_href: "http://www.mad.tv/madradio/interviews/",
		href:"http://www.mad.tv/madradio/interviews/"
	});*/
	
	/*
	 * VIDEO
	 */
	/*video.find('.sidetag').remove();
	video.find('h1').html('<a href="http://www.mad.tv/video/#cat=11">Video</a>');
	video.find('.content').load('/madradio/player/index.php');
	video.show();*/

	/*livenow.agendabox({
		header:"now on",
		href_more:'/madradio/program.php',
		data:data.liveradio,
		thumb_tag:"mid"
	})*/
	livenow.find(".sidetag").remove();
	livenow.find("h1").html("Studio Webcam");
	$.getJSON("/nc/madradiowebcast.php", function(resp){
		var streamstat = resp.streaming;
		if (streamstat) {
			livenow.find(".html_container").load('/madradio/livenowplayer.php');
		} else {
			var liveradio = data.liveradio;
			livenow.find(".msg").css("text-align","center");
			livenow.find(".msg").html('<span style="color:red; font-weight:600;">Now on-air:</span>&nbsp;'+liveradio.now.title);
			livenow.find(".html_container").html('<img src="/includes/theme/madradio/radiolive.png" />');
		}
	});
	livenow.show();

	exitpoll.boxlistchart({
		header:'exit poll',
		header_href:'/madradio/exitpoll.php',
		data:data.exitpoll,
		maxitems:25,
		//caption:'<img src="/includes/theme/madradio/xmasexitpoll.png" border="0" style="margin-left:38px;margin-top:-10px;">',
		//caption_href:'/madradio/exitpoll/',
		caption:'Δώσε την ψήφο σου και στήριξε το αγαπημένο σου τραγούδι',
		caption_href:'/madradio/exitpoll.php',
		caption_class:'box_list_char_caption',
		title_counter:true
	});
	exitpoll.show();
	if ($("div.chartlist", exitpoll).height() >= 200) {
		$("div.chartlist", exitpoll).height(($("div.chartlist", exitpoll).height() - 25));
		$("div.chartlist", exitpoll).jScrollPane({
			scrollbarOnLeft: false,
			scrollbarWidth: 15,
			dragMaxHeight: 42
		});
	}

	mappes.find('h1').css({'font-size':'1.6em'});
	mappes.singleBox({
		title:"",
		header: "Mappes Show – Best Of",
		header_href: "http://www.mad.tv/madradio/mappes/",
		image: "/includes/theme/madradio/3MAPPES.jpg",
		image_href: "http://www.mad.tv/madradio/mappes/",
		href:"http://www.mad.tv/madradio/mappes/"
	});

	//- TWITTER
	twitter.find('div.sidetag').remove();
	twitter.find("div.html_container").addClass("blackbg");
	twitter.find('h1').html('<a href="http://twitter.com/madtv/madradio" target="_blank"><span style="font-size:0.6em;">madradio</span>@twitter</a>');
    twitter.show();
	var xml = xmlParse(data.twitter);
	$(xml).each(function(i,n) {
		var tweet = $("div.html_container",twitter);
		var img = '<img src="'+n.img+'" border="0" width="35" style="margin-right:3px;">';
		var name = '<a href="https://twitter.com/#!/'+n.screen_name+'" target="_blank" style="color:#484848;"><strong>'+n.name+'</strong></a>';
		var screen_name = '<span style="margin-left:5px;color:#989898;font-size:0.8em;">'+n.screen_name+'</span>';
		tweet.append('<div style="margin:2px 0 0 10px;">'+img+name+screen_name+'</div><div style="margin:2px 0 17px 10px;font-size:0.8em;">'+n.text+'</div>');
	});
	if ($("div.html_container",twitter).height() >= 240) {
		$("div.html_container",twitter).height(250);
		$("div.html_container",twitter).width(295);
		//$("div.html_container",twitter).height(($("div.html_container", twitter).height() - 20));
		$("div.html_container",twitter).jScrollPane({
			scrollbarOnLeft: false,
			scrollbarWidth: 15,
			dragMaxHeight: 42
		});
	}

	special.quadbox({
         header:"specials",
		 data:data.special[0],
		 hovver_effect:false,
		 random:false,
		 maxitems:4
    });
    special.show();

	/*$.each(data.live,function(i,n){n.caption = n.title;n.image = convertImage(n.photo,"org");});
	live.splitBox({
		header: "mad live",
		data: data.live
	});*/
	
	/*promoBox.simplecatlist({
		categories:['news','tv','radio'],
		active:2,
		data:data.musictvradio,
		maxitems:6
	});*/
}

function xmlParse(data) {
	data = data.replace(/<rss version="2.0">/, '<?xml version="1.0"?>');
	data = data.replace(/<\/rss>/, '');
	var parser = new DOMParser();
	var xmlObject = $(parser.parseFromString(data,"text/xml"));
	var xmldata = [];
	xmlObject.find("status").each(function(i,n){
		var me = $(this);
		if ($.browser.msie || $.browser.webkit) {
			var text = me.find("text").text();
			var name = me.find("name").text();
			var screen_name = me.find("screen_name").text();
			var img = me.find("profile_image_url").text();
			var date = me.find("created_at").text();
			//var pat = /http:\/\/(.*)[\. |,]/i;
			//text = text.replace(pat,'<a href="http://$1">$1</a>');
			xmldata.push({
				text: text,
				name: name,
				screen_name: screen_name,
				img: img,
				date: date
			});
		} else {
			var text = (me.find("text").text()) ? me.find("text").text() : me.find("text").html();
			//var pat = /http:\/\/(.*)/
			//text = text.replace(pat,'<a href="http://$1">$1</a>');
			var name = (me.find("name").text()) ? me.find("name").text() : me.find("name").html();
			var screen_name = (me.find("screen_name").text()) ? me.find("screen_name").text() : me.find("screen_name").html();
			var img = (me.find("profile_image_url").text()) ? me.find("profile_image_url").text() : me.find("profile_image_url").html();
			var date = (me.find("created_at").text()) ? me.find("created_at").text() : me.find("created_at").html();
		}
		xmldata.push({
			text: text,
			name: name,
			screen_name: screen_name,
			img: img,
			date: date
		});
	});
	return xmldata;
}

function parseUrl1(data) {
    var e=/^((http|ftp):\/)?\/?([^:\/\s]+)((\/\w+)*\/)([\w\-\.]+\.[^#?\s]+)(#[\w\-]+)? $/;

    if (data.match(e)) {
        return  {url: RegExp['$&'],
                protocol: RegExp.$2,
                host:RegExp.$3,
                path:RegExp.$4,
                file:RegExp.$6,
                hash:RegExp.$7};
    }
    else {
        return  {url:"", protocol:"",host:"",path:"",file:"",hash:""};
    }
}
