// 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});

	$.getJSON(Proxies.homepage.homepage, function(resp) {
	//$.jsonP(Proxies.homepage.homepage, function(resp) {
		data = resp;
		//try {
			Render();
		//} catch(e) {
		//	alert("A problem has occured, please refresh");
		//}
	});

	loadCSSInclude("/includes/css/homepage.css");

});



function Render(){
	$("#content").html("");
	var newsrotation = $("#template_box_select").clone(true);
	var videoTabBox = $("#template_box_doubletab").clone(true);
    var newscats = $("#template_simplecategory").clone(true);
	var promoBox = $("#template_quad_box_images").clone(true);
	var fortybest = get("template_box_single");
	var agenda = $("#template_box_agenda").clone(true);
	var contest = $("#template_box_quad").clone(true);
	var poll = $("#template_box_poll").clone(true);
	var chart = $("#template_box_list_chart").clone(true);
	var cinema = $("#template_box_cinema").clone(true);
	var live = $("#template_box_quad").clone(true);
	var comments = get("template_box_list_comments_new");
	var twitter = $("#template_box_single_container").clone(true);

	// LINE 1
	$("#content").append(newsrotation);
	$("#content").append('<div class="boxespace8 left"></div>');
	$("#content").append(chart);
	$("#content").append('<div class="boxespace9 left"></div>');
	//$("#content").append(videoBox);
	$("#content").append(videoTabBox);
	newline();

	// LINE 2
	$("#content").append(newscats);
	$("#content").append('<div class="boxespace8 left"></div>');
	$("#content").append(twitter);
	$("#content").append('<div class="boxespace9 left"></div>');
	displayAdBox($("#content"),"boxA");
	newline();

	// LINE 3
	displayAdBox($("#content"),"boxB");
	$("#content").append('<div class="boxespace8 left"></div>');
	$("#content").append(poll);
	$("#content").append('<div class="boxespace9 left"></div>');
	$("#content").append(comments);
	newline();

	//- LINE 4
	$("#content").append(fortybest);
	$("#content").append('<div class="boxespace8 left"></div>');
	$("#content").append(promoBox);
	$("#content").append('<div class="boxespace9 left"></div>');
	$("#content").append(contest);
	newline();

	//- LINE 5
	$("#content").append(live);
	$("#content").append('<div class="boxespace8 left"></div>');
	$("#content").append(agenda);
	$("#content").append('<div class="boxespace9 left"></div>');
	$("#content").append(cinema);
	newline();

	// NEWS ROTATION
	newsrotation.selectBox({
		header: 'latest news',
		header_href: 'news.php',
		startpointer:0,
		size:9,
		data: data.news,
		show_summary:1,
		effect_hover:true
	});
	newsrotation.show();
	newsrotation.find('img').css({'width':'352'});
	newsrotation.find('.tcaption').css({'font-size':'0.8em'});


	//- VIDEO TAB BOX
	videoTabBox.singleBox({
		title: data.video[0].title,
		summary: "",
		header: "hot videos",
		header_href: "/video/?caID=20",
		image: convertImage(data.video[0].photobig,"big"),
		href:"/video/?caID=20",
		overlay:"/includes/theme/colors/"+SKIN+"/playimage.png"
	});
	videoTabBox.show();
	$('#tab1').click(function(){
		videoTabBox.singleBox({
			title: data.video[0].title,
			summary: "",
			header: "",
			header_href: "",
			image: convertImage(data.video[0].photobig,"big"),
			href:"/video/?caID=20",
			overlay:"/includes/theme/colors/"+SKIN+"/playimage.png"
		});
		$('#tab1',videoTabBox).attr({'class':'top_comments'});
		$('#tab2',videoTabBox).attr({'class':'top_chat'});
	});
	$('#tab2').click(function(){
		videoTabBox.singleBox({
			title: data.video[1].title,
			summary: "",
			header: "",
			header_href: "",
			image: convertImage(data.video[1].photobig,"big"),
			href:"/video/?caID=2",
			overlay:"/includes/theme/colors/"+SKIN+"/playimage.png"
		});
		$('#tab1',videoTabBox).attr({'class':'top_chat'});
		$('#tab2',videoTabBox).attr({'class':'top_comments'});
	});
	if ($(".captionbig",videoTabBox).height() > 21) {
		$("img#playover",videoTabBox).css("margin-bottom","-27px");
	}
	$('#tab1',videoTabBox).css('width','165px');


	//- CHARTS
	chart.chartBox({
		header:'charts',
		header_href:"/charts/",
		data:data.charts,
		maxitems:'1',
		sidetag:'',
		caption_class:'',
		number_caption:'<font size="-1">no</font>',
		caption:'Δες Περισσότερα',
		caption_href:"/charts/"
	});
	chart.show();
	if ($("div.chartlist", chart).height() >= 200) {
		$("div.chartlist", chart).height(($("div.chartlist", chart).height() + 30));
		$("div.chartlist", chart).jScrollPane({
			scrollbarOnLeft: false,
			scrollbarWidth: 15,
			dragMaxHeight: 42
		});
	}

    //- NEWS TV RADIO
	newscats.simplecatlist({
		categories:['news','tv','radio'],
		active:0,
		data:data.musictvradio,
		maxitems:6,
		stopheight:true
	});
	newscats.show();
	newscats.find('.list').css({'overflow':'hidden'});
	newscats.find('.list').height(259);

	//- TWITTER
	twitter.find('div.sidetag').remove();
	twitter.find("div.html_container").addClass("blackbg");
	twitter.find('h1').html('<a href="https://twitter.com/#!/madtv" target="_blank"><span style="font-size:0.6em;">madtv</span>@twitter</a>');
    twitter.show();
	var twitdata = data.twitter;
	$(twitdata.results).each(function(i,n) {
		var tweet = $("div.html_container",twitter);
		var img = '<img src="'+n.profile_image_url+'" border="0" width="35" style="margin-right:3px;">';
		var name = '<a href="https://twitter.com/#!/'+n.from_user_name+'" target="_blank" style="color:#484848;"><strong>'+n.from_user_name+'</strong></a>';
		var time_posted = '<span style="margin-left:5px;color:#989898;font-size:0.8em;">'+n.created_at+'</span>';
		tweet.append('<div style="margin:2px 0 0 10px;">'+img+name+time_posted+'</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
		});
		$(".jScrollPaneDrag",twitter).height(40);
	}

	//- POLL
	poll.pollBox({
		header:'poll',
		header_href:"/polls/",
		question:data.poll.question.question,
		data:data.poll,
		sidetag:"Vote"
	});
	poll.show();

	//- COMMENTS
	comments.commentBoxNew({
		header: 'comments',
		data: data.comments,
		maxitems:5, /*unilited*/
		sidetag: "",
		shortText:120,
		callback:function(){
			$("div.chartlist", comments).jScrollPane({
				scrollbarOnLeft: false,
				scrollbarWidth: 15,
				dragMaxHeight: 42
			});
		}
	});
	comments.show();
	if ($("div.chartlist", comments).height() >= 200) {
		$("div.chartlist", comments).height(($("div.chartlist", comments).height() + 50));
		$("div.chartlist", comments).jScrollPane({
			scrollbarOnLeft: false,
			scrollbarWidth: 15,
			dragMaxHeight: 42
		});
	}

	// 40 BEST 2nd LIST
	fortybest.singleBox({
		title:data.fortybest[0].title,
		header: 'mad music lists',
		//header_href: data.fortybest[0].href,
		header_href: 'http://www.mad.tv/listen/30best/',
		image: data.fortybest[0].photo.getImage("big"),
		image_href: data.fortybest[0].href,
		href:data.fortybest[0].href
	});
	fortybest.show();

	//- PROTASEIS
	promoBox.quadbox({
		header:"mad προτάσεις",
		header_href:"/section/promo/",
		data:data.promo[0],
		hovver_effect:true,
		random:false
    });
	promoBox.show();

	//- AGENDA
	agenda.agendabox({
		header:"Σήμερα",
		href_more:'http://www.mad.tv/agenda/',
		data:data.agenda,
		image_tag:"small",
		thumb_tag:"mid",
		agenda:true
	})
	agenda.show();

	//- CONTESTS
	$.each(data.contest,function(i,n){
		n.caption = n.title;
		n.image = convertImage(n.photo,"org");
	});
	contest.splitBox({
		header: "διαγωνισμοί",
		header_href: "/contests/",
		skip_inactive:true,
		data: data.contest
	});
	contest.show();

	//- LIVE
	live.find(".content").empty();
	live.find("h1").html("ζώδια");
	live.find(".content").append('<iframe src ="http://www.astrology.gr/modules/mod_zodiac/zodiac3.html" width="300" height="250" SCROLLING=NO frameborder="0" >');
	live.show();

	//- CINEMA
	var xml_cinemaRandom = cinemaParser(data.cinema);
	var pointer = Math.floor(Math.random() * xml_cinemaRandom.length);
	cinema.cinemaBox({
		header:"mad cinema",
		header_href:"/cinema/",
		image:xml_cinemaRandom[pointer].img,
		title:xml_cinemaRandom[pointer].title,
		director:xml_cinemaRandom[pointer].director,
		cast:xml_cinemaRandom[pointer].cast,
		movid:xml_cinemaRandom[pointer].movid
	})
	cinema.show();

}


function cinemaParser(cinemaData) {
	cinemaData = cinemaData.replace(/<rss version="2.0">/, '<?xml version="1.0"?>');
	cinemaData = cinemaData.replace(/<\/rss>/, '');
	var parser = new DOMParser();
	var xmlObject = $(parser.parseFromString(cinemaData,"text/xml"));
	var xml_cinema = [];
	xmlObject.find("item").each(function(i,n){
		var me = $(this);
		if ($.browser.msie || $.browser.webkit) {
			var title = me.find("title").text();
			var movurl = me.find("link").text();
			var description = me.find("description").text();
		} else {
			var title = (me.find("title").text()) ? me.find("title").text() : me.find("title").html();
 			var movurl = (me.find("link").text()) ? me.find("link").text() : me.find("link").html();
			var description = (me.find("description").text()) ? me.find("description").text() : me.find("description").html();
		}
		var splitUrl = movurl.split('n=');
		var movid = splitUrl[1].replace("&m=1","");
		var tmpSplit = description.split('<p>');
		var director = tmpSplit[1];
		var cast = tmpSplit[2];
		var summary = tmpSplit[3].replace(/]]-->/i, "");
		var img = me.find("enclosure").attr("url");
		xml_cinema.push({
			title: title,
			description: description,
			director: director,
			cast: cast,
			summary: summary,
			img: img,
			movid: movid
		});
	});
	return xml_cinema;
}

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:""};
    }
}
