//main menu 

var timeout	= 500;
var closetimer	= 0;
var ddmenuitem	= 0;

function mopen(el_id){

	mcancelclosetime();
	
	ddmenuitem = document.getElementById(el_id);
	
	ddmenuitem.style.display = "";

}

function mclose()
{
	if(ddmenuitem) ddmenuitem.style.display = 'none';
}

function mclosetime()
{
	closetimer = window.setTimeout(mclose, timeout);
}

// cancel close timer
function mcancelclosetime()
{
	if(closetimer)
	{
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

function toggleSlide(id_name){

	var el = document.getElementById(id_name);
	
	if(el.style.display=="none"){
		Effect.Appear(id_name, {duration:.5});
	}else{
		Effect.SlideUp(id_name, {duration:.5});
	}
}

function toggleReadMore(id_name,id_link){
	
	var el = document.getElementById(id_name);
	var el_link = document.getElementById(id_link);
	
	if(el.style.display=="none"){
		el.style.display="";
		el_link.innerHTML = "<a href=\"#\" onclick=\"toggleReadMore('"+id_name+"','"+id_link+"');return false;\">Hide</a>";
	}else{
		el.style.display="none";
		el_link.innerHTML = "<a href=\"#\" onclick=\"toggleReadMore('"+id_name+"','"+id_link+"');return false;\">Read More</a>";
	}	
	
}

function toggleShowMore(id_name,id_link){
	
	var el = document.getElementById(id_name);
	var el_link = document.getElementById(id_link);
	
	if(el.style.display=="none"){
		el.style.display="";
		el_link.innerHTML = "<a href=\"#\" onclick=\"toggleShowMore('"+id_name+"','"+id_link+"');return false;\">Hide</a>";
	}else{
		el.style.display="none";
		el_link.innerHTML = "<a href=\"#\" onclick=\"toggleShowMore('"+id_name+"','"+id_link+"');return false;\">Show More</a>";
	}	
	
}

var curContent;
var sliderOut=false;

function togglePlayerContent(content_num){
	
	var el_slider = document.getElementById("playerslider");
	foundItem = false;

	for(i=1;i<=6;i++){

		var el = document.getElementById('playercon'+i);
		if(i==content_num && curContent!=i){
			el.style.display="";
			foundItem = true;
		}else{
			el.style.display="none";
		}
	
	}
	
	curContent = content_num;
	
	if(foundItem && !sliderOut){ Effect.SlideDown(el_slider, {duration:.5}); sliderOut=true; }
	if(!foundItem && sliderOut){ Effect.SlideUp(el_slider, {duration:.5}); sliderOut=false; curContent=0; } 
	
}

var curContentNW;
var sliderOutNW=false;

function toggleNewsContent(content_num){
	
	var el_slider = document.getElementById("nwslider");
	foundItem = false;

	for(i=1;i<=3;i++){

		var el = document.getElementById('nwcon'+i);
		if(i==content_num && curContentNW!=i){
			el.style.display="";
			foundItem = true;
		}else{
			el.style.display="none";
		}
	
	}
	
	curContentNW = content_num;
	
	if(foundItem && !sliderOutNW){ Effect.SlideDown(el_slider, {duration:.5}); sliderOutNW=true; }
	if(!foundItem && sliderOutNW){ Effect.SlideUp(el_slider, {duration:.5}); sliderOutNW=false; curContentNW=0; } 
	
}

function toggleArtistContent(content_num){
	
	var el_slider = document.getElementById("playerslider");
	foundItem = false;

	for(i=1;i<=3;i++){

		var el = document.getElementById('playercon'+i);
		if(i==content_num && curContent!=i){
			el.style.display="";
			foundItem = true;
		}else{
			el.style.display="none";
		}
	
	}
	
	curContent = content_num;
	
	if(foundItem && !sliderOut){ Effect.SlideDown(el_slider, {duration:.5}); sliderOut=true; }
	if(!foundItem && sliderOut){ Effect.SlideUp(el_slider, {duration:.5}); sliderOut=false; curContent=0; } 
	
}

var toggled = false;
function expandAllListing(classname){
	
	if(toggled == false){
		var els = getElementsByClassName(classname);
		for(var i=0;i<els.length;i++){
			els[i].style.display = "";	
		}
		toggled = true;
		var expand = document.getElementById('expand');
		expand.style.display = 'none';
		var collapse = document.getElementById('collapse');
		collapse.style.display = '';
	}else if(toggled == true){
		var els = getElementsByClassName(classname);
		for(var i=0;i<els.length;i++){
			els[i].style.display = "none";	
		}
		toggled = false;
		var expand = document.getElementById('expand');
		expand.style.display = '';
		var collapse = document.getElementById('collapse');
		collapse.style.display = 'none';
	}
	
}

function launchSendToFriend(filename, width){
		var args = "filename="+filename;
		window.open("/sendtofriend.html?mode=video&"+args+"", 'Photo_Popup', 'width='+width+', height=400, top=125, screenY=125, left=150, screenX=150, menubar=no, toolbar=no, location=no, directories=no, scrollbars=yes, resizable=yes');

}

function launchSendPicToFriend(filename, width){
		var args = "filename="+filename;
		window.open("/sendtofriend.html?mode=photo&"+args+"", 'Photo_Popup', 'width='+width+', height=400, top=125, screenY=125, left=150, screenX=150, menubar=no, toolbar=no, location=no, directories=no, scrollbars=yes, resizable=yes');

}

function launchSendPlaylistToFriend(filename, width){
		var args = "filename="+filename;
		window.open("/sendtofriend.html?mode=playlist&"+args+"", 'Photo_Popup', 'width='+width+', height=400, top=125, screenY=125, left=150, screenX=150, menubar=no, toolbar=no, location=no, directories=no, scrollbars=yes, resizable=yes');

}

function launchContestTerms(contest){
	window.open("/contest_terms.html?contest_name="+contest+"", 'Contest_Terms', 'width=500, height=400, top=125, screenY=125, left=150, screenX=150, menubar=no, toolbar=no, location=no, directories=no, scrollbars=yes, resizable=yes');
	
}

function launchSendArticleToFriend(filename, width){
		var args = "filename="+filename;
		window.open("/sendtofriend.html?mode=article&"+args+"", 'Photo_Popup', 'width='+width+', height=450, top=125, screenY=125, left=150, screenX=150, menubar=no, toolbar=no, location=no, directories=no, scrollbars=yes, resizable=yes');

}

function getElementsByClassName(classname) {

node = document.getElementsByTagName("body")[0];

var a = [];

var re = new RegExp('\\b' + classname + '\\b');

var els = node.getElementsByTagName("*");

for(var i=0,j=els.length; i<j; i++)

if(re.test(els[i].className))a.push(els[i]);

return a;

}

var count = 1;
//homepage ajax function to return more random videos to the top videos area
function getMoreVideos(){
	offset = count * 8;
	var opt = {
		// Use POST
		method: 'get',
		// Send this lovely data
		//postBody: escape('offset=8'),
		// Handle successful response
		onComplete: function(t){ 
			moreVideosGot(t);
		},
		onSuccess: function(t) {
			moreVideosGot(t);
		},
		// Handle 404
		on404: function(t) {
			alert('Error 404: location "' + t.statusText + '" was not found.');
		},
		// Handle other errors
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		}
	}
	new Ajax.Request('/morevideos.php?offset='+offset, opt);
	count++;
}

function moreVideosGot(t){
	
	var xmlDoc = t.responseXML;
		
	var xmlItems = xmlDoc.getElementsByTagName('video');
		
	var newhtml = "";
	
	for (var i=0;i<xmlItems.length;i++){

			var videolink = xmlItems[i].getAttribute("videolink");
			var artistlink = xmlItems[i].getAttribute("artistlink");
			var thumbnail = xmlItems[i].getAttribute("thumbnail");
			var artistname = xmlItems[i].getAttribute("artistname");
			var videoname = xmlItems[i].getAttribute("videoname");
			var playlist_add0 = xmlItems[i].getAttribute("playlist_add0");
			var playlist_add1 = xmlItems[i].getAttribute("playlist_add1");
			var playlist_add2 = xmlItems[i].getAttribute("playlist_add2");
			var playlist_add = playlist_add0+'?'+playlist_add1+playlist_add2;
			newhtml += "<li><div class='item'><div class='tn'>"+playlist_add+"<a href='"+videolink+"'><img src='"+thumbnail+"'  alt='"+artistname+" - "+videoname+"' /></a></div><h2><a href='"+artistlink+"'>"+artistname+"</a></h2><h3><a href='"+videolink+"'>"+videoname+"</a></h3></div></li>";
	
	}		
	var el = document.getElementById('newvideos');
	el.innerHTML = newhtml;
	lightwindowInit();
}

//Utility Functions
function copytoClipboard(text){
	text.execCommand("Copy");
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

//Search Functions
//Search Functions
function track_search(page, search_hash, url){
	var opt = {
		// Use POST
		method: 'get',
		// Send this lovely data
		//postBody: escape('offset=8'),
		// Handle successful response
		onComplete: function(t){ 
			search_tracked();
		},
		// Handle 404
		on404: function(t) {
			alert('Error 404: location "' + t.statusText + '" was not found.');
		},
		// Handle other errors
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		}
	}
	new Ajax.Request('/track_click.php?page='+encodeURIComponent(page)+'&shash='+search_hash+'&search_url='+encodeURIComponent(url), opt);
}

function search_tracked(){
	
}

function track_ringtone(page, url){
	var opt = {
		// Use POST
		method: 'get',
		// Send this lovely data
		//postBody: escape('offset=8'),
		// Handle successful response
		onComplete: function(t){ 
			ringtone_tracked();
		},
		// Handle 404
		on404: function(t) {
			alert('Error 404: location "' + t.statusText + '" was not found.');
		},
		// Handle other errors
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		}
	}
	new Ajax.Request('/track_click.php?page='+encodeURIComponent(page)+'&search_url='+encodeURIComponent(url), opt);
}

function ringtone_tracked(){

}

function add_member_group(user_ID, artist){
	var opt = {
		// Use POST
		method: 'get',
		// Send this lovely data
		//postBody: escape('offset=8'),
		// Handle successful response
		onComplete: function(t){ 
			fan_added(t);
		},
		// Handle 404
		on404: function(t) {
			alert('Error 404: location "' + t.statusText + '" was not found.');
		},
		// Handle other errors
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		}
	}
	new Ajax.Request('/artist_ajax.php?mode=add&user_ID='+user_ID+'&artist='+artist, opt);
}

function fan_added(){
	becomefan = document.getElementById('becomefan');
	notfan = document.getElementById('notfan');
	fan = document.getElementById('fan');
	becomefan.style.display = 'none';
	notfan.style.display = 'none';
	fan.style.display = '';
	
	var numfans = $('numfans');
	var num = numfans.innerHTML;
	num = parseInt(num.replace(/\,/, ''));
	numfans.innerHTML = addCommas(num+1); 

}

function remove_member_group(user_ID, artist, id){
        var opt = {
                // Use POST
                method: 'get',
                // Send this lovely data
                //postBody: escape('offset=8'),
                // Handle successful response
                onComplete: function(t){
                        //alert(t.responseText);
                        //row = document.getElementById(id);
                        //row.style.display = 'none';
                        Effect.Fade(id, {duration: .7});
                },
                // Handle 404
                on404: function(t) {
                        alert('Error 404: location "' + t.statusText + '" was not found.');
                },
                // Handle other errors
                onFailure: function(t) {
                        alert('Error ' + t.status + ' -- ' + t.statusText);
                }
        }
        new Ajax.Request('/artist_ajax.php?mode=delete&user_ID='+user_ID+'&artist='+artist, opt);
}

function add_member_group2(user_ID, artist){
	var opt = {
		// Use POST
		method: 'get',
		// Send this lovely data
		//postBody: escape('offset=8'),
		// Handle successful response
		onComplete: function(t){ 
			fan_added2(t);
		},
		// Handle 404
		on404: function(t) {
			alert('Error 404: location "' + t.statusText + '" was not found.');
		},
		// Handle other errors
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		}
	}
	new Ajax.Request('/artist_ajax.php?mode=add&user_ID='+user_ID+'&artist='+artist, opt);
}

function fan_added2(){
	//becomefan = document.getElementById('becomefan');
	notfan = document.getElementById('notfan');
	fan = document.getElementById('fan');
	//becomefan.style.display = 'none';
	notfan.style.display = 'none';
	fan.style.display = '';
}

function newsletter_add(){
	var opt = {
		// Use POST
		method: 'get',
		// Send this lovely data
		//postBody: escape('offset=8'),
		// Handle successful response
		onComplete: function(t){ 
			newsletter_added(t);
		},
		// Handle 404
		on404: function(t) {
			alert('Error 404: location "' + t.statusText + '" was not found.');
		},
		// Handle other errors
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		}
	}
	var email_address = document.getElementById('form_text');
	new Ajax.Request('/newsletter_ajax.php?email_address='+email_address.value, opt);
}

function newsletter_added(){
	//becomefan = document.getElementById('becomefan');
	newsletter_successmessage = document.getElementById('newsletter_successmessage');
	newsletter_errormessage = document.getElementById('newsletter_errormessage');
	footerboxnewsletter = document.getElementById('footerboxnewsletter');

	//newsletter_successmessage = '';
	newsletter_successmessage.style.display = '';
	newsletter_successmessage.innerHTML = "Thanks for signing up!  You will receive a confirmation email shortly.";
	footerboxnewsletter.style.display = 'none';
}

function reportProblem(){
	var opt = {
		// Use POST
		method: 'get',
		// Send this lovely data
		//postBody: escape('offset=8'),
		// Handle successful response
		onComplete: function(t){ 
			 problem_reported(t);
		},
		// Handle 404
		on404: function(t) {
			alert('Error 404: location "' + t.statusText + '" was not found.');
		},
		// Handle other errors
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		}
	}

	var requiredMajorVersion = 8;
	// Minor version of Flash required
	var requiredMinorVersion = 0;
	// Minor version of Flash required
	var requiredRevision = 0;

	var flashvar;
	flashvar = GetSwfVer();
	var problem = document.getElementById('playerdebugger');
	new Ajax.Request('/log_problem.php?flashver='+flashvar+'&problem='+problem.innerHTML, opt);
}

function problem_reported(){
	addbug = document.getElementById('playercon6');
	togglePlayerContent(6);
	bugform = document.getElementById('bugform');
	new Effect.Highlight(bugform, { startcolor: "#ff0000",
duration: 2});
	bugform.innerHTML = 'Thank you! <br/>We are aware of your problem and we will do our best to resolve it immediately.';
	

}
