// indexOf for Array searching... IE sucks
if(!Array.indexOf){
	Array.prototype.indexOf = function(obj){
		for(var i=0; i<this.length; i++){
			if(this[i]==obj){
				return i;
			}
		}
		return -1;
	}
}

function getGameAndGeneralInfo(id,type,element) {
	if (type == 'general') {
		type = 'exhibitor';
	}

	$.getJSON('/'+ type+ '/view/' + id + '?format=json', function(data) {
		$("#game-and-general-info-container").html(info_panel_markup(data,type));		
	});

	$("ul.listBox li").removeClass("selected");
	$(element).parent().addClass("selected");
}

function getContent(type, id, exhibitorid, gamename, gameid, element) {
	if(typeof(deepLinkShareURL) != 'undefined') deepLinkShareURL = "";
	if($("#mpDiv").length > 0){jwplayer('mpDiv').stop();}
	var url = '/' + type + '/view/';
	if(id == exhibitorid){
		url += id + '/undefined?format=json';
	}
	else{
		url += id + '/' + exhibitorid + '?format=json';
	}
	$.getJSON(url, function(data) {
		showContent(data, type, element, gamename, gameid, exhibitorid);
	});
}
function showContent(data, type, element, gamename, gameid, exhibitorid) {
	var shareURL = "http://www.e3insider.com/exhibitor/view/" + exhibitorid + "/";
	$("#article-content-container").html(detail_panel_markup(data, type, gamename, gameid, shareURL));
	e3Tabs.closeBoxes();
	$("#prodMenu li").removeClass("selected");
	$(element).parent().addClass("selected");
}
function detail_panel_markup(data,type,gamename,gameid,baseShareURL) {
		var html = "";
		var socialURL;
		var gameNameParsed = gamename.toLowerCase().replace(/[^a-zA-Z0-9 ]/g,"").replace(/ /g,"-");
		var shareTitle = "E3 Insider | ";
		var thumbsPreloaded = 8;
		var currentlyVisible = 0;
		var listenersAdded = false;
		var maxThumbs = 0;
		switch(type) {
			case 'article':
				socialURL = baseShareURL + gameNameParsed + "/" + gameid + "/" + data.id;
				shareTitle += data.title;
//console.log("share url: " + socialURL);
				var date = formateThisDate(new Date(data.date));
				
				var body = data.body;
				html += '				<div class="socialIcons"></div>';
				html += '<div id="article_container">';
				html += '	<div class="customScrollBox">';

				html += '			<div class="container">';
				html += '				<div class="content">';
				html += "				<h2>" + date + "</h2>";
				html += body;
				html += '				</div>';
				html += '			</div>';
				html += '			<div class="dragger_container">';
				html += '				<div class="dragger"></div>';
				html += '			</div>';
				html += '	</div>';
				html += '	<a href="#" class="scrollUpBtn" onclick="return false;"></a> <a href="#" class="scrollDownBtn"></a>';
				html += '	<span id="thumbCount"></span>';
				html += '</div>';
				
				setTimeout(function(){
					//sweep through UL to see if titles match.  If so, highlight it
					$("#info_panel #prodMenu .articleLink a").each(function(){
						if($(this).html() == data.title){
							$(this).parent().addClass('selected');
						}
					});
//					util.fixFlash();  //uncomment if editors are not embedding videos with wmode specified
					$("#article_container").mCustomScrollbar("vertical",500,"easeOutCirc",1.2,"auto","yes","yes",20);
					if($("a[rel^='prettyPhoto']").length >0){
						$("a[rel^='prettyPhoto']").prettyPhoto({
							theme: 'light_rounded', /* light_rounded / dark_rounded / light_square / dark_square / facebook */
							show_title: false, /* true/false */
							allow_resize: true,
							overlay_gallery: false
						});
					}
					loadSocial(socialURL,shareTitle);
					
				},500);
				break;
			case 'image':
				if (!!data.images) {
				socialURL = baseShareURL + gameNameParsed + "/" + gameid + "/-1/-1/" + data.images[0].id;
				shareTitle += gamename + " Screenshots";
//console.log(socialURL);
//console.log("share title: " + shareTitle);
					html += '				<div class="socialIcons"></div>';
					maxThumbs = data.images.length;
					html += '<div id="screen-box" class="normal">';
					html += '	<ul class="screen-container">';
					for (i = 0; i < maxThumbs; i++) {
						if (i < thumbsPreloaded) {
							currentlyVisible++;
							if (i == 0) {
					html += '		<li class="first selected"><a href="http://cdn1.gamepro.com' +data.images[i].urlPath+ '" rel="prettyPhoto[screenshots]"><img src="http://cdn1.gamepro.com' +data.images[i].urlPath+ '" alt="" /></a></li>';
							} else {
					html += '		<li><a href="http://cdn1.gamepro.com' +data.images[i].urlPath+ '" rel="prettyPhoto[screenshots]"><img src="http://cdn1.gamepro.com' +data.images[i].urlPath+ '" alt="" /></a></li>';
							}
						}
					}
					html += '	</ul>';
					html += '	<a class="prev"><</a><a class="next">></a>';
					html += '</div>';
					
					html += '<div id="thumbnail_container">';
					html += '	<div class="customScrollBox">';
					html += '		<div class="horWrapper">';
					html += '			<div class="container">';
					html += '				<div class="content">';
					html += '					<ul class="thumb-container">';
					//load first 8 images only, then after scrolling past the 8, load rest by ajax
					for (i = 0; i < maxThumbs; i++) {
						if (i < thumbsPreloaded) {
							if (i == 0) {
					html += '						<li class="first selected"><img src="http://cdn1.gamepro.com' +data.images[i].thmPath+ '" alt="" rel="'+data.images[i].id+'"></li>';
							} else {
					html += '						<li><img src="http://cdn1.gamepro.com' +data.images[i].thmPath+ '" alt="" rel="'+data.images[i].id+'"></li>';
							}
						}
					}
					html += '					</ul>';
					html += '				</div>';
					html += '			</div>';
					html += '			<div class="dragger_container">';
					html += '				<div class="dragger"></div>';
					html += '			</div>';
					html += '		</div>';
					html += '	</div>';
					html += '	<a href="#" class="scrollUpBtn" onclick="return false;"></a> <a href="#" class="scrollDownBtn"></a>';
					html += '	<a href="#" class="moreBtn" onclick="return false;">View More &raquo;</a> <a href="#" class="allBtn" onclick="return false;">View All &raquo;</a>';
					html += '	<span id="thumbCount"></span>';
					html += '</div>';
				}
				setTimeout(function(){
					initAddOns();
					$("#info_panel #prodMenu .screens").addClass('selected');
					loadSocial(socialURL,shareTitle);
				},500);
				break;
			case 'video':
				if (!!data.videos) {
					socialURL = baseShareURL + gameNameParsed + "/" + gameid + "/-1/" + data.videos[0].id;
					shareTitle += data.videos[0].name;
//console.log(socialURL);
//console.log("share title: " + shareTitle);

					html += '<div class="socialIcons"></div>';


					maxThumbs = data.videos.length;
					html += '<div id="screen-box" class="normal">';
					html += '	<ul class="screen-container">';
					html += '		<li class="first selected">';
					html += '			<div id="mpDiv">';
					//VIDEO WILL BE EMBEDDED HERE
					html += '			</div>';				
					html += '		</li>';
					html += '	</ul>';
					html += '</div>';

					html += '<div id="thumbnail_container">';
					html += '	<div class="customScrollBox">';
					html += '		<div class="horWrapper">';
					html += '			<div class="container">';
					html += '				<div class="content">';
					html += '					<ul class="thumb-container">';
					//load first 8 images only, then after scrolling past the 8, load rest by ajax			
					for (i = 0; i < maxThumbs; i++) {
						if (i < thumbsPreloaded) {
							currentlyVisible++;
							if (i == 0) {
								html += '<li class="first selected"><img alt="' +data.videos[i].name+ '" title="' +data.videos[i].name+ '" src="http://cdn1.gamepro.com/video/hdr/' +data.videos[i].thumbfile+ '"></li>';
							} else {
								html += '<li><img alt="' +data.videos[i].name+ '" title="' +data.videos[i].name+ '" src="http://cdn1.gamepro.com/video/hdr/' +data.videos[i].thumbfile+ '"></li>';
							}
						}
					}
					html += '					</ul>';
					html += '				</div>';
					html += '			</div>';
					html += '			<div class="dragger_container">';
					html += '				<div class="dragger"></div>';
					html += '			</div>';
					html += '		</div>';
					html += '	</div>';
					html += '	<a href="#" class="scrollUpBtn" onclick="return false;"></a> <a href="#" class="scrollDownBtn"></a>';
					html += '	<a href="#" class="moreBtn" onclick="return false;">View More &raquo;</a> <a href="#" class="allBtn" onclick="return false;">View All &raquo;</a>';
					html += '	<span id="thumbCount"></span>';
					html += '</div>';
				}
				
				setTimeout(function(){
					initAddOns();
					$("#info_panel #prodMenu .videos").addClass('selected');
					loadSocial(socialURL,shareTitle);
				},500);
				//load first movie after mpDiv exists
				setTimeout(function(){
					videoEmbed.loadIt('mpDiv',data.videos[0].id,'352','221');
				},500);
				break;
		}
		return html;
		
		function initAddOns(){
			//Custom Scroller
			$("#thumbnail_container").mCustomScrollbar("horizontal",500,"easeOutCirc",1,"auto","yes","yes",20);
			gallery.init(type);
			if(type == "image"){
				//LightBox Photo Gallery
				$("a[rel^='prettyPhoto']").prettyPhoto({
					theme: 'light_rounded', /* light_rounded / dark_rounded / light_square / dark_square / facebook */
					show_title: false, /* true/false */
					allow_resize: true,
					overlay_gallery: false
				});
			}
			//update thumbCount
			$("#thumbCount").html('[' + currentlyVisible + ' of ' + maxThumbs +']');
			//add listeners to buttons
			if(listenersAdded == false && currentlyVisible < maxThumbs){
				$("#thumbnail_container .moreBtn").bind("click",viewMore).css({color:'#f70023',cursor:'pointer'});
				$("#thumbnail_container .allBtn").bind("click",viewAll).css({color:'#f70023',cursor:'pointer'});
				listenersAdded = true;
			}
		}
		//View More Button Pressed
		function viewMore(){
			var highlightThumb = currentlyVisible;
			if(currentlyVisible < maxThumbs){
				for(x = currentlyVisible; x < maxThumbs && x < currentlyVisible + thumbsPreloaded; x++){
					if(type == "video"){
						$("#thumbnail_container .thumb-container").append('<li><img alt="' +data.videos[x].name+ '" title="' +data.videos[x].name+ '" src="http://cdn1.gamepro.com/video/hdr/' +data.videos[x].thumbfile+ '" alt=""></li>');
					} else {
						$("#screen-box .screen-container").append('<li><a href="http://cdn1.gamepro.com' +data.images[x].urlPath+ '" rel="prettyPhoto[screenshots]"><img src="http://cdn1.gamepro.com' +data.images[x].urlPath+ '" alt="" /></a></li>');
						$("#thumbnail_container .thumb-container").append('<li><img src="http://cdn1.gamepro.com' +data.images[x].thmPath+ '" alt=""></li>');
					}
				}
				currentlyVisible = x;
				setTimeout(initAddOns,500);
				setTimeout(function(){gallery.selectSlide(highlightThumb + 1)},500);
				//if showing all images, then disable button
				if(currentlyVisible >= maxThumbs){
					$("#thumbnail_container .moreBtn").css({color:'#cccccc',cursor:'default'});
					$("#thumbnail_container .allBtn").css({color:'#cccccc',cursor:'default'});
				}
			} 
			return false;
		}
		//View All Button Pressed
		function viewAll(){
			if(currentlyVisible < maxThumbs){
				for(x = currentlyVisible; x < maxThumbs; x++){
					if(type == "video"){
						$("#thumbnail_container .thumb-container").append('<li><img alt="' +data.videos[x].name+ '" title="' +data.videos[x].name+ '" src="http://cdn1.gamepro.com/video/hdr/' +data.videos[x].thumbfile+ '" alt=""></li>');
					} else {
						$("#screen-box .screen-container").append('<li><a href="http://cdn1.gamepro.com' +data.images[x].urlPath+ '" rel="prettyPhoto[screenshots]"><img src="http://cdn1.gamepro.com' +data.images[x].urlPath+ '" alt="" /></a></li>');
						$("#thumbnail_container .thumb-container").append('<li><img src="http://cdn1.gamepro.com' +data.images[x].thmPath+ '" alt=""></li>');
					}
				}
				currentlyVisible = x;
				setTimeout(initAddOns,500);
				//if showing all images, then disable button
				if(currentlyVisible >= maxThumbs){
					$("#thumbnail_container .moreBtn").css({color:'#cccccc',cursor:'default'});
					$("#thumbnail_container .allBtn").css({color:'#cccccc',cursor:'default'});
				}
			} 
			return false;
		}
}


function loadSocial(socialURL, shareTitle){
	if(typeof(deepLinkShareURL) != 'undefined' && deepLinkShareURL.length > 0) socialURL = deepLinkShareURL;
	$("#article-content-container .socialIcons").empty().html('<div class="socialURL">'+socialURL+'</div><div class="shareTitle">'+shareTitle+'</div><div class="tweet"><a href="http://twitter.com/share" class="twitter-share-button" data-url="' + socialURL + '" data-text="' + shareTitle + '" data-count="horizontal" data-via="E3Insider">Tweet</a><sc'+'ript type="text/javasc'+'ript" src="http://platform.twitter.com/widgets.js"></sc'+'ript></div><div class="fb"><iframe src="http://www.facebook.com/plugins/like.php?href=' + socialURL + '&amp;send=false&amp;layout=button_count&amp;width=110&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=arial&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:110px; height:21px;" allowTransparency="true"></iframe></div>');
}

function info_panel_markup(data,type) {
	var html = "";
	if(!data) { return html; }
	if(typeof(data) == "string") { data = eval('(' + data + ')'); }

	var tabName = 'General Stories';
	var tabClass = 'generalInfo';
	if (type == 'game') {
		tabName = 'Game Info';
		tabClass = 'gameInfo';
	}
	
	if(data.id==null || data.id=='' || data.id=='undefined'){
		var gameid = '-1';
	} else {
		var gameid = data.id;
	}

	var gamename = data.title.replace(/\'/g,"\\'");
	var gameNameParsed = gamename.toLowerCase().replace(/[^a-zA-Z0-9 ]/g,"").replace(/ /g,"-");

	var releaseDT = 'TBD';
	if(!!data.release_dt) {
		releaseDT = data.release_dt;
	}

	html += '<li class="top"></li>';
	html += '<li class="middle"></li>';
	html += '<li class="bottom"></li>';
	html += '<li class="tabTitle"><a href="##" onclick="return false;" class="' + tabClass + '">'+ tabName + '</a></li>';
	html += '<li class="tabContent" id = "content-info">';
	html += '	<div id="info_panel">';
	html += '	<div class="customScrollBox">';
	html += '		<div class="container">';
	html += '			<div class="content">';
	html += '				<div class="prodDetails">';
	if(!!data.title) {
		html += "<h3>" + data.title + "</h3>";
	}
	if (type == 'game') {
		html += '<ul class = "platformList">';
		if(!!data.platforms && !!data.platforms.length && data.platforms.length > 0) {
			
			for(var i = 0; i < data.platforms.length; i++) {
				html += '<li>'+ data.platforms[i] + '</li>';
			}
		}
		html += '<li class = "releaseDate"><strong>Release Date:</strong> ' +releaseDT+ '</li>';
		html += '</ul>';
		html += '<div id="voteBox" class="' + data.id + '"><ul><li><a href = "#" class = "good">Good</a></li><li><a href="#" class="better">Better</a></li><li><a href="##" class="best">Best</a></li><li class="whatDoYouThink">What Do You Think?</li><li class="pointer"></li></ul></div>';
	}	
	
	if(!!data.blurb) {
		html += '<p id="prodDesc">' +data.blurb+ '</p>';
	}
	html += '				</div>';
	html += '<ul id = "prodMenu">';	
	if(!!data.articles && !!data.articles.length && data.articles.length > 0) {
		html += '<li class="articleCount">ARTICLES <span>('+ parseInt(data.articles.length) + ')</span></li>';
		for(var i = 0; i < data.articles.length; i++) {
			var art_call = "getContent('article'," + data.articles[i].id + "," + data.exhibitorid + ",'" + gamename + "'," + gameid + ",this)";
			html += '<li class="articleLink"><a href="#" onClick="' + art_call + ';return false;">' + data.articles[i].title + '</a></li>';
		}
	}
	if(!!data.videos && data.videos.count > 0) {
		var art_call = "getContent('video'," + data.videos.id + ","+ data.exhibitorid + ",'" + gamename + "'," + gameid + ",this)";
		html += '<li class="videos"><a href="#" onClick="' +art_call+';return false;">VIDEOS &nbsp;&amp;&nbsp; TRAILERS <span>(' + parseInt(data.videos.count) + ')</span></a></li>';
	}
	if (type == 'game') {
		if(!!data.images && data.images.count > 0) {
			var art_call = "getContent('image'," + data.images.id + ","+ data.exhibitorid + ",'" + gamename + "'," + gameid + ",this)";
			html += '<li class="screens"><a href="#" onClick="' +art_call+';return false;">SCREENSHOTS &nbsp;&amp;&nbsp; ART <span>(' + parseInt(data.images.count) + ')</span></a></li>';
		}
	}
	html += '</ul>';
	html += '			</div>';
	html += '		</div>';
	html += '		<div class="dragger_container">';
	html += '			<div class="dragger"></div>';
	html += '		</div>';
	html += '		<a href="##" class="scrollUpBtn"></a>';
	html += '		<a href="##" class="scrollDownBtn"></a>';
	html += '		</div>';
	html += '	</div>';
	html += '</li>';
	setTimeout(function(){
		$("#info_panel").mCustomScrollbar("vertical",400,"easeOutCirc",1.2,"auto","yes","yes",20);
		voteBox.init();
	},500);

	return html;
}
