// JavaScript Document

var focus_function;

function get(id) {
	return document.getElementById(id);
}

function updateSiteWidgetDisplay(index) {
	if(!index) { index = 0;
	get('fullButtonSite').innerHTML="<img src=\"images/buttons/full_button_off.png\" style=\"padding-left: 4px\"/>";
	get('listButtonSite').innerHTML= "<img src=\"images/buttons/list_button_on.png\" onMouseOver=\"this.src='images/buttons/list_button_over.png'\" onMouseOut=\"this.src='images/buttons/list_button_on.png'\" onclick=\"updateSiteListDisplay()\"/>";
	}

	if(index>=sitesInfo.length) { index = 0;}
	get('siteWidgetScreen').innerHTML = sitesInfoHtml(index);
}

function updateDL (track_id) {
	var active = document.getElementById('DL:'+track_id).value;
	if(active) { active = "active"; } else { active = ""; }  

	var callback = function () {
		window.location.reload();
	}

	var XObj;
	try { XObj = new XMLHttpRequest(); }
	catch(e) { XObj = new ActiveXObject(Microsoft.XMLHTTP); }
	
	XObj.onreadystatechange = function () {	
	if(XObj.readyState == 4) {
			if(callback) {
				callback();
			}
		}
	}		
	
	XObj.open('POST','php/sharing.php?track_id=' + track_id + '&active=' + active, true);
	XObj.send(null);
}

function updateSiteListDisplay() {
	get('siteWidgetScreen').innerHTML = sitesListHtml();
	get('listButtonSite').innerHTML="<img src=\"images/buttons/list_button_off.png\"/>";
	get('fullButtonSite').innerHTML="<img src=\"images/buttons/full_button_on.png\" onMouseOver=\"this.src='images/buttons/full_button_over.png'\" onMouseOut=\"this.src='images/buttons/full_button_on.png'\" style=\"padding-left: 4px\" onClick=\"updateSiteWidgetDisplay(0)\"/>";
}

function sitesListHtml() {
	var html = "<div>";
	for(var i=0; i < sitesInfo.length; i++) {
		var site = sitesInfo[i];		
		html += "<div style='padding-bottom: 3px'>";
		html += "<a class='blue_link' href='" + site['url'] + "'>" + site['title'] + "</a>";
		html += " <span class='grey'>" + site['link'] + "</span></div>";
	}
	html += "</div>";
	return html;
}

function sitesInfoHtml(index) {
	var new_index = index + 1;
	var site = sitesInfo[index];
	var html = "<div>";
	html += "<div style='float: left; clear: both; z-index: 0; margin: 6px 6px 2px 6px'><img style='border: 1px solid #CCC' src='" + site['img'] + "' width='120px'/></div>";
	html += "<div style='z-index: 0'><span style='font-family: impact; font-size: 18px'>" + site['title'] + "</span><br />";
	html += "<div style='float: left; width: 150px; margin: 0px 0px 8px 0px'>";

	html += site['bio'].substr(0, 110) + "...";
	
	html += "</div>";
	html += "<div style='float: left; margin: 0px 0px 6px 0px'>";
	html += "<span class='grey'>Site:</span> <a class='blue_link' href='" + site['url'] + "'>" + site['title'] + "</a><br />";
	html += "<span class='grey'>BigUps:</span> <b>" + site['bigups'] + "</b><br />";
	html += "<span class='grey'>Launch:</span> " + make_date(site['launch']);
	html += "</div>";

	html += "<div class='info_button' style='z-index: 0; clear: both; margin: 14px 0px 0px 0px' onClick='updateSiteWidgetDisplay(" + (new_index) + ")'><center>next</center></div>";
	html += "</div>";
	return html;
}

function sendTopicToTwitter (topic_id, link) {
	var html_form = "<textarea style='width: 100%; height: 22px' id='twitterUpdate:" + topic_id + "'>check this out: http://" + link + "</textarea><br />"; 
	html_form += "<span class='submitTitle'>Username: </span><input class='basic_input' id='twitterName:" + topic_id + "'/><br />";
	html_form += "<span class='submitTitle'>Password: </span><input class='basic_input' id='twitterPassword:" + topic_id + "'/><br />";
	html_form += "<input value='Update' type='button' onClick='updateTwitter(" + topic_id + ")'/>";
	document.getElementById('postOut:'+topic_id).innerHTML = html_form;
}

// ---
// written by John Resig
// http://ejohn.org/blog/javascript-pretty-date/
// ---
function prettyDate(time){
  var values = time.split(" ");
  time = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
  var ms = Date.parse(time) - (new Date()).getTimezoneOffset() * 60000;
  if (ms < 60) {
    return '<b>Less than a minute ago</b>';
	}
  else if (ms < 60*60) {
    return '<b>' +(parseInt(ms / 60)).toString()+' minutes ago</b>';
  } 
 else if (ms < 120*60) {
    return '<b>About an hour ago</b>';
  } 
  return ( new Date(ms) ) .toDateString();


	var date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," ")),
	    diff = (((new Date()).getTime() - date.getTime()) / 1000),
	    day_diff = Math.floor(diff / 86400);
			
	if ( isNaN(day_diff) || day_diff < 0 || day_diff >= 31 )
		return;
			
	return day_diff == 0 && (
			diff < 60 && "just now" ||
			diff < 120 && "1 minute ago" ||
			diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" ||
			diff < 7200 && "1 hour ago" ||
			diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") ||
		day_diff == 1 && "Yesterday" ||
		day_diff < 7 && day_diff + " days ago" ||
		day_diff < 31 && Math.ceil( day_diff / 7 ) + " weeks ago";
}

function jsonTweets (tweets) {
	var html = "";
	var i = 0;
	for(i=0; i<=5; i++) {
		html += "<div style='margin-bottom: 6px'>";
		var tweet = tweets[i];
		html += formatTweet(tweet.text); 
		html += "<span style='color: #666; font-size: 10px'>" + prettyDate(tweet.created_at);
		html += " from " + tweet.source + "</span>";
		html += "</div>";
	}
	return html;
}

function formatTweet (tweet) {
	var words = tweet.split(' ');
	var html = "<div>";
	for (var i in words) {
		var www = words[i].indexOf('http://');
		var url = words[i].indexOf('www');
		var ext = words[i].indexOf('.');
		var reply = words[i].indexOf('@');
		var tag = words[i].indexOf('#');

		if(www>=0) {
			var link = words[i].split('http://');
			if(link[1]) {
			 var tmp = link[0];
			 tmp += " <a class='blue_link' href='http://" + link[1] + "'>" + link[1] + "</a>";	
			 link = tmp;
			} else {
			 link = words[i];
			 link = "<a class='blue_link' href='" + words[i] + "'>" + link + "</a>";
			}
			words[i] = link;
		} else if(url>=0&&ext>=0) {
			var link = words[i];
			words[i] = "<a class='blue_link' href='" + words[i] + "'>" + link + "</a>";
		}
	
		if (reply >= 0 && words[i].length > 1) {
			var name = words[i].substring(1);
			words[i] = "@<a class='blue_link' href='http://twitter.com/" + name + "'>" + name + "</a>";
		} else if (reply > 0 && !ext && words[i].length > 1) {
			var name = words[i].substring(1);
			words[i] = "@<a class='blue_link' href='http://twitter.com/" + name + "'>" + name + "</a>";
		}

		if (tag >= 0 && words[i].length > 1) {
			var name = words[i].substring(1);
			words[i] = "<a class='blue_link' href='http://twitter.com/#search?q=%23" + name + "'>" + words[i] + "</a>";
		}
		html += words[i] + " ";
	}
	html += "</div>";
	return html;
}

function getRawTwitter () {
	var callback = function (text) {
		if(!text) {
			getRawTwitter();
		}

		var jsonObj = eval('('+text+')');
		document.getElementById('twitterWrapper').innerHTML = jsonTweets(jsonObj); 
		return false;

		//alert(jsonObj[0].text);
		//alert(jsonObj[0].created_at);
		//alert(jsonObj[0].source);
		//alert(jsonObj[0].profile_image_url);
	}

	var XObj;
	try { XObj = new XMLHttpRequest(); }
	catch(e) { XObj = new ActiveXObject(Microsoft.XMLHTTP); }
	
	XObj.onreadystatechange = function () {	
	if(XObj.readyState == 4) {
			if(callback) {
				callback(XObj.responseText);
			}
		}
	}		
	
	XObj.open('POST','php/twitter_user.php', true);
	XObj.send(null);
}

function updateTwitter (topic_id) {
	var password = document.getElementById('twitterPassword:'+topic_id).value;
	var name = document.getElementById('twitterName:'+topic_id).value;
	var update = document.getElementById('twitterUpdate:'+topic_id).value;

	if(!update) {
		alert('You must enter text to send updates.');
		return false;
	}

	if(!name || !password) {
		alert('You must enter your twitter username and password.');
		return false;
	}

	var callback = function (text) {
		var jsonObj = eval('('+text+')');
		document.getElementById('postOut:'+topic_id).innerHTML = "<b>Update Posted</b>:<Br /><span style='color: #999; font-weight: bold'>''" + jsonObj.text + "''</span>";
	}

	var XObj;
	try { XObj = new XMLHttpRequest(); }
	catch(e) { XObj = new ActiveXObject(Microsoft.XMLHTTP); }
	
	XObj.onreadystatechange = function () {	
	if(XObj.readyState == 4) {
			if(callback) {
				callback(XObj.responseText);
			}
		}		
	}		
	
	XObj.open('POST','php/twitter.php?name=' + name + '&pass=' + password + '&txt=' + escape(update), true);
	XObj.send(null);
}

function post_form_update (tabIndex, form) {
  if(!auth) {
    return false;
  }

  for (var i=0;i<=2;i++) {
    document.getElementById('postButton_'+i).className = 'grey';
  }
  document.getElementById('postButton_'+tabIndex).className = '';
  document.getElementById('postForm').innerHTML = postFormContent(form);
}

function postFormContent(formName) {
  switch(formName) {
  case 'video': var html = "<input type='hidden' name='video' id='video' value='true'/>"
		  + "<input style='width: 340px; margin-left: 35px; color: #BBB' id='video_title' name='video_title' value='title...' onfocus='if(this.value==\"title...\") { this.value=\"\"; } this.style.color=\"#000\"' class='basicInput' id='new_topic_name'/><br /><input id='video_src' name='video_src' style='width: 340px; margin-left: 35px; color: #BBB;' onfocus='if(this.value==\"location...\") { this.value=\"\"; } this.style.color=\"#000\"' value='location...' class='basicInput'><br />";
		  html += "<input class='info_button' type='submit' style='margin-left: 35px' onClick='add_video()' value='Post'/>";
		  html += "<input class='info_button' type=\"submit\" onClick=\"window.location.reload()\" value=\"Cancel\"/>";
    break;

  case 'event': var html = "<div style='margin-left: 45px'><input type='hidden' name='event' id='event' value='true'/>"
		+ "<input style='width: 340px; margin-left: 25px; color: #BBB' class='basicInput' onfocus='if(this.value==\"title...\") { this.value=\"\"; } this.style.color=\"#000\"' name='event_name' id='event_name' value='title...'/>"
		+ "<br /><input style='width: 340px; margin-left: 25px; color: #BBB' class='basicInput' name='event_date' id='event_date' onfocus='if(this.value==\"event date YYYY-MM-DD (2009-12-31)...\") { this.value=\"\"; } this.style.color=\"#000\"' value='event date YYYY-MM-DD (2009-12-31)...'/>"
		+ "<br /><input style='width: 340px; margin-left: 25px; color: #BBB' class='basicInput' name='event_img' id='event_img' onfocus='if(this.value==\"image location...\") { this.value=\"\"; } this.style.color=\"#000\"' value='image location...'/>"
		+ " <div style='font-size: 10px; margin-left: 25px'>paste image location (http://www...)</div>"
		+ "<br /><textarea onfocus='if(this.value==\"message...\") { this.value=\"\"; } this.style.color=\"#000\"' style='width: 340px; margin-left: 25px; color: #BBB' class='basicInput' name='event_info' id='event_info'/>message...</textarea>"
		+ "<br /><input onfocus='if(this.value==\"doors open...\") { this.value=\"\"; } this.style.color=\"#000\"' style='width: 340px; margin-left: 25px; color: #BBB' class='basicInput' name='event_door' id='event_door' value='doors open...'/>"
		+ "<br /><input onfocus='if(this.value==\"show starts...\") { this.value=\"\"; } this.style.color=\"#000\"' style='width: 340px; margin-left: 25px; color: #BBB' class='basicInput' name='event_start' id='event_start' value='show starts...'/>"
		+ " <div style='font-size: 10px; margin-left: 25px'><hr style='border: 1px dotted #CCC'/> Pre-Sale Tickets Link (<span class='grey'>optional</span>): <input name='event_sales' id='event_sales' class='basicInput'/><br />"
		+ "<input class='info_button' type='submit' onClick='add_event()' value='Add'/>"
		+ "<input class=\"info_button\" type=\"submit\" onClick=\"window.location.reload()\" value=\"Cancel\"/></div>";
     break;
   default: var html = "<div style='margin-left: 45px'><input style='width: 340px; margin-left: 35px; color: #BBB' value='title...' onfocus='if(this.value==\"title...\") { this.value=\"\"; } this.style.color=\"#000\"' class='basicInput' id='new_topic_name'/><br /><textarea id='new_topic_info' style='width: 340px; margin-left: 35px; color: #BBB;' onfocus='if(this.value==\"message...\") { this.value=\"\"; } this.style.color=\"#000\"'>message...</textarea><br />"
	          + '<span class="info_button" onclick="new_topic();" style="margin: 2px 2px 2px 35px; padding: 2px"> Post </span></div>';
  }
    return html;
}

function subLogin (tag) {

	var email = document.getElementById('inputEmail'+tag).value;
	var pass = document.getElementById('inputPass'+tag).value;
	
	if(!email || !pass) {
		loginFail('login_message_'+tag, 'You must enter your email AND password to login.');
		return false;
	}

	autoLogin(email, pass, tag);
}

function loginFail (domName, message) {
	get(domName).style.display = "block";
	get(domName).innerHTML = message;
}

function resetForm(tag) {
	var html = "confirm email:<input id='resetEmail" + tag + "'/><br />new password:<input id='resetPass" + tag + "' type='password' /><br /><input type='button' style='background-color: #FFF; width: 60px; cursor: pointer' value='Reset' onClick='sendPassword(" + tag + ")'/>";
	get('login_input_'+tag).innerHTML = html;
	get('login_message_'+tag).innerHTML = "";
}


function sendPassword (tag) {
	var email = document.getElementById('resetEmail'+tag).value;
	var pass = document.getElementById('resetPass'+tag).value;
	
	if(!email || !pass) {	
		loginFail('login_message_'+tag, 'You must confirm your email AND enter a new password to login.');
		return false;
	}

	var callback = function (txt) {
		if(txt.reponseText) { loginFail('login_message_'+tag, 'Sorry, the email you provided did not match our records...'); return false; }
		get('login_input_'+tag).innerHTML = "Check inbox to confirm your email and login with your new password.";
		get('login_message_'+tag).innerHTML = "";
	}

	var XObj;
	try { XObj = new XMLHttpRequest(); }
	catch(e) { XObj = new ActiveXObject(Microsoft.XMLHTTP); }

	XObj.onreadystatechange = function () {	
	if(XObj.readyState == 4) {
			if(callback) {
				callback(XObj);
			}
		}		
	}		
	
	XObj.open('POST','php/reg.php?reset=1&email='+email+'&passwd='+pass,true);
	XObj.send(null);
}

function autoLogin (email, pass, tag) {
	var callback = function (txt) {
		if(txt.responseText) {
			var ERROR = txt.responseText + " <a onClick='resetForm(\"" + tag + "\")'>Forgot Password?</a>";
	                loginFail('login_message_'+tag, ERROR);
			return false;
		}
		window.location="./profile.php";
	}
	
	var XObj;
	try { XObj = new XMLHttpRequest(); }
	catch(e) { XObj = new ActiveXObject(Microsoft.XMLHTTP); }
	
	XObj.onreadystatechange = function () {	
	if(XObj.readyState == 4) {
			if(callback) {
				callback(XObj);
			}
		}		
	}		
	
	XObj.open('POST','php/login.php?email=' + email + '&passwd=' + pass, true);
	XObj.send(null);	
}

function submitLogout () {
	var callback = function () {
		window.location.reload();
	}
	
	var XObj;
	try { XObj = new XMLHttpRequest(); }
	catch(e) { XObj = new ActiveXObject(Microsoft.XMLHTTP); }
	
	XObj.onreadystatechange = function () {	
	if(XObj.readyState == 4) {
			if(callback) {
				callback();
			}
		}		
	}		
	
	XObj.open('POST','php/login.php?logout=true', true);
	XObj.send(null);	
}

var new_account_type = 0;

var y = 0;
var start_speed = 8;
var speed = start_speed;
var rate = 30;
var width = 360;
var max_width = 94;
var max_album_width = 310;
var stop_slide = false;

function doc(id) {
	return document.getElementById(id);
}

function add_item_price (album_id) {
	var form = "<input style='width: 220px' id='item_price_input:" + album_id + "'/><input type='button' value='Add Price' onClick='submit_item_price(" + album_id + ")'/>";
	document.getElementById("item_price:" + album_id).innerHTML = form;
}

function submit_item_price (album_id) {
	var	album_price = document.getElementById("item_price_input:" + album_id).value;
	
	var callback = function () {
		document.getElementById('item_price:' + album_id).innerHTML	= "$" + album_price;
	}
	
	var XObj;
	try { XObj = new XMLHttpRequest(); }
	catch(e) { XObj = new ActiveXObject(Microsoft.XMLHTTP); }
	
	XObj.onreadystatechange = function () {
		if(XObj.readyState == 4) {
			if(callback) {
				callback();
			}
		}		
	}		
	
	XObj.open('POST','php/add_album.php?update_price=true&price=' + album_price + '&album_id=' + album_id, true);
	XObj.send(null);	
}


function update_item_info (album_id) {
	var form = "<div style='border-top: 1px dotted #333'><textarea id='submit_item_text:" + album_id + "' style='width: 320px'></textarea><br /><input type='button' onClick='submit_item_data(" + album_id + ")' value='Add'/> <input type='button' value='Cancel' onClick='window.location.reload()'/></div><br />";
	document.getElementById('item_text:' + album_id).innerHTML = form;
}

function submit_item_data (album_id) {
	var data = document.getElementById('submit_item_text:' + album_id).value;
	
	var callback = function () {
		window.location.reload();		
	}
	
	var XObj;
	try { XObj = new XMLHttpRequest(); }
	catch(e) { XObj = new ActiveXObject(Microsoft.XMLHTTP); }
	
	XObj.onreadystatechange = function () {
		if(XObj.readyState == 4) {
			if(callback) {
				callback();
			}
		}		
	}		
	
	XObj.open('POST','php/add_album.php?update_info=true&text=' + data + '&album_id=' + album_id, true);
	XObj.send(null);	
}

function post_item() {
	document.getElementById('new_items').innerHTML =
		  "<input type='hidden' name='item' id='album' value='true'/>"
		+ "Item Name <br /><input style='width:100%' name='item_title' id='item_title'/><br />"
		+ "Artwork<br /><input style='width:100%' name='item_img' id='item_img'/><br />"
		+ " <span style='font-size: 10px'>paste image url (http://www...)</span><br />"
		+ "Item Type: <select id='item_type_select'><option value='5000'>CD</option><option value='5001'> DVD</option><option value='5002'> Poster</option></select><br />"
		//+ "<input type='checkbox' id='input_2' value='5001'/> DVD<br />"
		+ "<input type='submit' onClick='add_item()' value='Add'/>"
		+ "<input type=\"submit\" onClick=\"reset_post_item()\" value=\"Cancel\"/>";
}
	
function reset_post_item () {
	document.getElementById('new_items').innerHTML = "<span onClick='post_item()'>+ New Item</span>";
}

function remove_item(id) { 
	var callback = function () {
		window.location.reload();		
	}
	
	var XObj;
	try { XObj = new XMLHttpRequest(); }
	catch(e) { XObj = new ActiveXObject(Microsoft.XMLHTTP); }
	
	XObj.onreadystatechange = function () {
		if(XObj.readyState == 4) {
			if(callback) {
				callback();
			}
		}		
	}		
	
	XObj.open('POST','php/add_item.php?remove=true&id='+id,true);
	XObj.send(null);
}

function add_item() { 
	var title = document.getElementById('item_title').value;
	var img = document.getElementById('item_img').value;
	var type = document.getElementById('item_type_select').value;
	
	var callback = function () {
		window.location.reload();		
	}
	
	var XObj;
	try { XObj = new XMLHttpRequest(); }
	catch(e) { XObj = new ActiveXObject(Microsoft.XMLHTTP); }
	
	XObj.onreadystatechange = function () {
		if(XObj.readyState == 4) {
			if(callback) {
				callback();
			}
		}		
	}		
			
	XObj.open('POST','php/add_item.php?add=true&title=' + title + '&img=' + img + '&type=' + type, true);
	XObj.send(null);
}

function account_type_signup () {
	window.location = 'account.php?account_type=' + new_account_type;
}

function account_type (index) {
	new_account_type = index;
	reset_account_type();
	if(index==0) {document.getElementById('account_type:').checked = true; }
	document.getElementById('account_type:'+index).checked = true;
	document.getElementById('account_type').value = document.getElementById('account_type:'+index).value;
}

function reset_account_type () {
	var count = 3;
	for(var i =0; i < count; i++) {
		document.getElementById('account_type:'+i).checked = '';
	}
	document.getElementById('account_type:').checked = '';
}

function write_label_drop (index, album_id) {
	var html = "<div style='background-color: #FFF'>";
	html += "<select onchange='submit_bio_label(" + album_id + ", this.value)'>";
	for(var x=0;x<labels[index].length;x++) {
		if(labels[index][x]['default'] != 'custom') {
			html += "<option value='" + labels[index][x]['id'] + "'>" + labels[index][x]['value'] + "</option>";
		}
	}
	//html += "<option value='' style='border-top: 1px dotted #CCC; background-color: #EEE'>Other...</option>";
	html += "</select>";
	html += "</div>";
	document.getElementById('add_label:' + index).innerHTML = html;
}

function submit_bio_label(artist_id, label_id) {
	
	var callback = function () {
		window.location.reload();		
	}
	
	var XObj;
	try { XObj = new XMLHttpRequest(); }
	catch(e) { XObj = new ActiveXObject(Microsoft.XMLHTTP); }
	
	XObj.onreadystatechange = function () {
		if(XObj.readyState == 4) {
			if(callback) {
				callback();
			}
		}		
	}
	
	XObj.open('POST','php/profile.php?bio_label=true&artist_id=' + artist_id + '&label_id=' + label_id, true);
	XObj.send(null);
}

function make_date_time (date_time_string) {
	var string = date_time_string.split(" ");
	var date = make_date(string[0]);
	var time = make_time(string[1]);
	return time + "<br />" + date; 
}

function make_date (date_string) {
	var calender = date_string.split("-");
	var year = calender[0];
	var month = calender[1];
	var day = calender[2].split(" ");
	day = day[0];

	var month_name = new Array();
	month_name['01'] = "January";
	month_name['02'] = "Febraury";
	month_name['03'] = "March";
	month_name['04'] = "April";
	month_name['05'] = "May";
	month_name['06'] = "June";
	month_name['07'] = "July";
	month_name['08'] = "August";
	month_name['09'] = "September";
	month_name['10'] = "October";
	month_name['11'] = "November";
	month_name['12'] = "December";
	
	return "<b>" + month_name[month] + " " + day + "</b><span style='font-size: 10px'>, " + year + "</span>";
}

function make_time (time_string) {
	var day = time_string.split(":");
	var hour = day[0];
	var mins = day[1];
	var sec = day[2];
	
	if(hour > 12) {
		hour = hour - 12;
	}
	return hour + ":" + mins;
}
