/* general*/  

 var http_request = false;

   function makePOSTRequest(url, parameters) {

      http_request = false;

      if (window.XMLHttpRequest) { // Mozilla, Safari,...

         http_request = new XMLHttpRequest();

         if (http_request.overrideMimeType) {

         	// set type accordingly to anticipated content type

            //http_request.overrideMimeType('text/xml');

            http_request.overrideMimeType('text/html');

         }

      } else if (window.ActiveXObject) { // IE

         try {

            http_request = new ActiveXObject("Msxml2.XMLHTTP");

         } catch (e) {

            try {

               http_request = new ActiveXObject("Microsoft.XMLHTTP");

            } catch (e) {}

         }

      }

      if (!http_request) {

         alert('Cannot create XMLHTTP instance');

         return false;

      }

      

      http_request.onreadystatechange = alertContents;

      http_request.open('POST', url, true);

      http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

      http_request.setRequestHeader("Content-length", parameters.length);

      http_request.setRequestHeader("Connection", "close");

      http_request.send(parameters);

   }



   function alertContents() {

      if (http_request.readyState == 4) {

         if (http_request.status == 200) {

            //alert(http_request.responseText);

            result = http_request.responseText;

            document.getElementById('mycommentspan').innerHTML = result;

         } else {

            //alert('There was a problem with the request.');

			document.getElementById('mycommentspan').innerHTML = 'There was a problem with the request. Please contact us and report this problem!';

         }

      }

   }

   

   function get(obj) {

	   

	  var poststr;

	  if(document.getElementById("user_id").value == 0)

	  {

		 poststr = "comment_txt=" + encodeURI( document.getElementById("comment_txt").value ) +

                    "&vid=" + encodeURI( document.getElementById("vid").value ) +

					"&user_id=" + encodeURI( document.getElementById("user_id").value ) +

					"&username=" + encodeURI( document.getElementById("username").value ) +

					"&captcha=" + encodeURI( document.getElementById("captcha").value );

	  }

	  else {

		 poststr = "comment_txt=" + encodeURI( document.getElementById("comment_txt").value ) +

		 			"&user_id=" + encodeURI( document.getElementById("user_id").value ) +

                    "&vid=" + encodeURI( document.getElementById("vid").value );

	  }

	  

      makePOSTRequest(''+MELODYURL+'/comment.php', poststr);

   }



function submitonce(theform){

//if IE 4+ or NS 6+

	if(document.getElementById("user_id").value != 0)

	{

		if(document.getElementById("comment_txt").value == '' || document.getElementById("comment_txt").value == "test"){

			//alert('You need to write a comment first!');

		}

		else{

			if (document.all||document.getElementById){

				//screen thru every element in the form, and hunt down "submit" and "reset"

				for (i=0;i<theform.length;i++){

					var tempobj=theform.elements[i]

					if(tempobj.type.toLowerCase()=="submit"||tempobj.type.toLowerCase()=="reset")

					//disable em

					tempobj.disabled=true;

				}

				//theform.style.display='none';

				$(theform).slideUp('normal');

			}

		}

	}

	else

	{

		//	guest comment

		var alert_msg = '';

		if(document.getElementById("comment_txt").value == ''){

			//alert('You need to write a comment first!');

			alert_msg = alert_msg+'You need to write a comment first!\n';

		}

		if(document.getElementById("username").value == '' || document.getElementById("captcha").value == ''){

			alert_msg = alert_msg+'Please fill in the required fields.\n';

		}

		

		if(alert_msg != '') {

			//alert(alert_msg);

		}

		else{

			if (document.all||document.getElementById){

				//screen thru every element in the form, and hunt down "submit"

				for (i=0;i<theform.length;i++){

					var tempobj=theform.elements[i]

					if(tempobj.type.toLowerCase()=="submit")

					//disable em

					tempobj.disabled=false;

				}

				//theform.style.display='none';

				$(theform).slideUp('normal');

			}

		}	

	}

	

}



function validateSearch(b_on_submit){

    if(document.forms['search'].keywords.value == '' || document.forms['search'].keywords.value == 'search'){

        alert('You did not enter a search term. Please try again.');

        if(b_on_submit == 'true')

            return false;

    }

    else{

        document.forms['search'].submit();

    }

}

function lookup(inputString) {

	if(inputString.length == 0) {

		// Hide the suggestion box.

		$('#suggestions').hide();

	} else if(inputString.length > 2) {

		$.post(MELODYURL2+'/ajax_search.php', {queryString: ""+inputString+""}, function(data){

			if(data.length >0) {

				$('#suggestions').show();

				$('#autoSuggestionsList').html(data);

			}

		});

	}

} // lookup

	

function catlist()

{

	$("#ul_categories li").hoverIntent(

	 function(){

			var ul_subs = $(this).find('ul');

			

			if(ul_subs.hasClass("hidden_li"))

			{	

				ul_subs.removeClass();

				ul_subs.slideDown(80);

				ul_subs.addClass('visible_li')

			}

	 }, 

	 function(){

			var ul_subs = $(this).find('ul');

			

			if(ul_subs.hasClass("visible_li"))

			{

				ul_subs.addClass("hidden_li");	

				ul_subs.slideUp(150);

				ul_subs.removeClass('visible_li');

			}

	 });



}



function fill(thisValue) {

	$('#inputString').val(thisValue);

	setTimeout("$('#suggestions').hide();", 200);

}



function ajax_request(page, extra_params, output_sel, type, show) {

	

	var ret = false;

	

	if (type.length == 0)

	{

		type = "html";	

	}



	if (output_sel.length > 0)

	{

		$(output_sel).html('<img src="'+TemplateP+'/images/ajax-loading.gif" alt="Loading" id="loading" />Loading...').fadeIn('normal');	

	}

	$.ajax({

		   type: "GET",

		   url: MELODYURL2 + "/ajax.php", 

		   //cache: false, 

		   data: "p=" + page + "&" + extra_params,

		   dataType: type,

		   success: function(data) {

						if (output_sel.length > 0)

						{

							$(output_sel).html(data);

							

							if (show == true)

							{

								$(output_sel).show();

							}

						}

						ret = true;

					}

		   });

	

	return ret;

}



function onpage_delete_comment(comment_id, video_id, selector) {



	var confirm_msg = "You are about to delete this comment. Click 'Cancel' to stop, 'OK' to delete";

	var response = false;

	

	if (confirm(confirm_msg)) 

	{

		if (selector.length > 0) 

		{

			ajax_request("comments", "do=onpage_delete_comment&cid=" + comment_id + "&vid=" + video_id, "", "", false);

			$(selector).fadeOut('normal');

		}

	}

}



function onpage_delete_favorite(video_id, selector) {



	var response = false;



		if (selector.length > 0) 

		{

			ajax_request("favorites", "do=onpage_delete_favorite&vid=" + video_id, "", "", false);

			$(selector).fadeOut("slow");

		}

}

var arPlayer = null;

var arDone = false;

function reportNotFound(uid) {

	if (uid.length > 0 && !arDone) {

		ajax_request("video", "do=report&vid=" + uid, "", "", false);

		arDone = true;

	}

}

function onStreamNotFound(clip) {

	reportNotFound(clip.name);	

}

function triggerPlayerError(obj) {

	reportNotFound(arPlayer.id);

}

function playerReady(obj) {

	arPlayer = document.getElementById(obj['id']);

	arPlayer.addModelListener("ERROR", "triggerPlayerError");

}


/* --comments-- */


$(document).ready(function(){
	
	$("#preview_comment").hide();
	
	$("#c_submit").click(function(){
		
		//	hide the button
		//$(this).hide();
		
		//	display the 'loading' gif;
		$("#mycommentspan").html('<img src="'+TemplateP+'/images/ajax-loader.gif" alt="Loading" id="loading" />').show();

		//	get the info
		var userId = $("#c_user_id").val();
		var videoId	= $("#c_vid").val();
		var commentTxt = $("#c_comment_txt").val();
		var uName = $("#c_username").val();
		var captchaCode = $("#captcha").val();
		
		if(userId == 0)
		{
			// guest comment
			$.post( MELODYURL2+"/comment.php", { username: uName, captcha: captchaCode, vid: videoId, user_id: userId, comment_txt: commentTxt },
   					function(data){
						if(data.cond == true)
						{
							$("#myform").slideUp("normal", function() {
								$("#mycommentspan").html(data.msg).show();
								
								//	preview comment
								if(data.preview == true)
								{
									$comment = data.html;
									$comment = $comment.replace(/\n/g, "<br />").replace(/\n\n+/g, '<br /><br />');
									$("#be_the_first").hide();
									$("#preview_comment").html($comment).fadeIn(700);
								}
							});
						}
						else if(data.cond == false)
						{
							$("#c_submit").show();
						 	$("#mycommentspan").html(data.msg).show();									
						}
						
   					},"json"
				 );
		}
		else if(userId > 0)
		{
			// user comment
			$.post( MELODYURL2+"/comment.php", { vid: videoId, user_id: userId, comment_txt: commentTxt },
   					function(data){
						
						if(data.cond == true)
						{
							$("#myform").slideUp("normal", function() {  
								$("#mycommentspan").html(data.msg).show();
								
								//	preview comment
								if(data.preview == true)
								{
									$("#be_the_first").hide();										
									$("#preview_comment").html(data.html).fadeIn(700);
								}
							});
							
						}
						else if(data.cond == false)
						{
							$("#c_submit").show();
						 	$("#mycommentspan").html(data.msg).show();
						}
						
   					},"json"
				 );
		}
      return false;

	});	
});



/* --favorites-- */

function getfav(obj) {

  var poststr = "video_id=" + encodeURI( document.getElementById("video_id").value ) +

				"&user_id=" + encodeURI( document.getElementById("user_id").value );

  makePOSTRequest(''+MELODYURL+'/ajax_favorite.php', poststr);

}


/* --resize-- */

$(document).ready(function(){

  // Reset Font Size

  var originalFontSize = $('#pm_resizer').css('font-size');

  $(".resetFont").click(function(){

  $('#pm_resizer').css('font-size', originalFontSize);

  });

  // Increase Font Size

  $(".increaseFont").click(function(){

  	var currentFontSize = $('#pm_resizer').css('font-size');

 	var currentFontSizeNum = parseFloat(currentFontSize, 10);

    var newFontSize = currentFontSizeNum*1.2;

	$('#pm_resizer').css('font-size', newFontSize);

	return false;

  });

  // Decrease Font Size

  $(".decreaseFont").click(function(){

  	var currentFontSize = $('#pm_resizer').css('font-size');

 	var currentFontSizeNum = parseFloat(currentFontSize, 10);

    var newFontSize = currentFontSizeNum*0.8;

	$('#pm_resizer').css('font-size', newFontSize);

	return false;

  });

});




/* language selector */


$(document).ready( function() {

$(".lang_selected").click( 

 function() {

	var submenu = $(".lang_submenu");

	if( submenu.css("display") == "block" )

	{

		submenu.css( "display", "none" );

		$(this).removeClass();

		$(this).addClass("lang_selected");

	}

	else

	{

		submenu.css( "display", "block" );

		$(this).removeClass();

		$(this).addClass("lang_selected_onclick");

	}

 } 

);



$("a[id^='lang_select_']").each(

 function() {

  var id = parseInt( this.name );

  var lang = $('#lang_select_' + id);

  lang.click(

	function()

	{

	 $.post( MELODYURL2+"/index.php", { select_language: 1, lang_id: id }, function() { window.location.reload(); }, '');

	});

 });

});



/* pop up */

function report(URL) {

day = new Date();

id = day.getTime();

eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=yes,location=0,statusbar=0,menubar=0,resizable=yes,width=450,height=300,left = 100,top = 50');");

}



function reportPopup(ID, vid)

{

this.id=ID

this.vid=vid

window.open(''+MELODYURL+'/report.php?vid='+this.vid+'&do='+this.id, 'report','toolbar=0,scrollbars=yes,location=0,statusbar=0,menubar=0,resizable=yes,width=450,height=300,left=100,top=50');

}

function sharePopup(vid)

{

this.vid=vid

window.open(''+MELODYURL+'/share.php?vid='+this.vid, 'share','toolbar=0,scrollbars=no,location=0,statusbar=0,menubar=0,resizable=no,width=450,height=300,left=100,top=50');

}

function makeFavorite(vid)

{

this.vid=vid

window.open(''+MELODYURL+'/favorites.php?a=add&vid='+this.vid, 'share','toolbar=0,scrollbars=no,location=0,statusbar=0,menubar=0,resizable=no,width=450,height=150,left=100,top=50');

}



function addmyspacePopup(vid)

{

this.vid=vid

window.open(''+MELODYURL+'/share_code.php?vid='+this.vid+'&social=myspace', 'share','toolbar=0,scrollbars=no,location=0,statusbar=0,menubar=0,resizable=no,width=450,height=300,left=100,top=50');

}



function textLimit(field, maxlen) {

if (field.value.length > maxlen + 1)

alert('Your comment is too long!');

if (field.value.length > maxlen)

field.value = field.value.substring(0, maxlen);

} 

function SelectAll(id)

{

    document.getElementById(id).focus();

    document.getElementById(id).select();

}



/* upload */

 $(document).ready(function() {



	  $('#upload-video-form').submit(function() {

		var upFormFileSel 	= $('#upload-video-form input[name=mediafile]');

		var upFormCaptureSel= $('#upload-video-form input[name=capture]');

		var upFormCategorySel = $('#upload-video-form select[name=category]');

		var upFormTitleSel	= $('#upload-video-form input[name=video_title]');

		var upFormArtistSel	= $('#upload-video-form input[name=artist]');

		var upFormFile		= upFormFileSel.val();

		var upFormCapture	= upFormCaptureSel.val();

		var upFormCategory	= upFormCategorySel.val();

		var upFormTitle 	= upFormTitleSel.val();

		var upFormArtist	= upFormArtistSel.val();

		var allow 	= new Array('flv', 'mov', 'avi', 'divx', 'mp4', 'wmv', 'mkv', 'asf', 'wma', 'mp3', 'm4v', 'm4a', '3gp', '3g2');

		var ext 	= upFormFile.split('.').pop().toLowerCase();

		var allowCapture = new Array('jpg', 'jpeg', 'gif', 'png');

		var extCapture	= upFormCapture.split('.').pop().toLowerCase();



		if (upFormFile.length == 0 || upFormCapture.length == 0 || upFormCategory == "-1" || upFormTitle.length == 0 || upFormArtist.length == 0) {

			if (upFormFile.length == 0) {

				upFormFileSel.css({'background-color':'#FD8888'});

			} else {

				upFormFileSel.css({'background-color':'#FFFFFF'});

			}

			if (upFormCapture.length == 0) {

				upFormCaptureSel.css({'background-color':'#FD8888'});

			} else {

				upFormCaptureSel.css({'background-color':'#FFFFFF'});

			}

			if (upFormCategory == "-1")	{

				upFormCategorySel.css({'background-color':'#FD8888'});

			} else {

				upFormCategorySel.css({'background-color':'#FFFFFF'});

			}			

			if (upFormTitle.length == 0) {

				upFormTitleSel.css({'background-color':'#FD8888'});

			} else {

				upFormTitleSel.css({'background-color':'#FFFFFF'});

			}

			if (upFormArtist.length == 0) {

				upFormArtistSel.css({'background-color':'#FD8888'});

			} else {

				upFormArtistSel.css({'background-color':'#FFFFFF'});

			}			

			alert("Please fill in the required, highlighted fields.");

			

			return false;

		} else {

			upFormFileSel.css({'background-color':'#FFFFFF'});

			upFormCategorySel.css({'background-color':'#FFFFFF'});

			upFormTitleSel.css({'background-color':'#FFFFFF'});

			upFormArtistSel.css({'background-color':'#FFFFFF'});

		}		



		if(jQuery.inArray(ext, allow) == -1) {

			upFormFileSel.css({'background-color':'#FD8888'});

		    alert('Invalid extension!');

			return false;

		}

					

		if(jQuery.inArray(extCapture, allowCapture) == -1) {

			upFormCaptureSel.css({'background-color':'#FD8888'});

		    alert('Invalid extension!');

			return false;

		}		

		

		$('#Submit').hide();

		$('#uploading_gif').html('<img src="'+ TemplateP +'/images/ajax-loader.gif" id="loading" /> Uploading...').show();

	

		return true;

	  });

	

  });


/* ajaxtabs */


$(document).ready( function () {

	$("#tabs_sameartist").hide();

	$("#tabs_other").hide();

	$("#tabs_related").show();

	

	

	$("#tabs_select_related").click( function() {

		

		 var vid = $(this).find('a').attr('class');

		 var divs = $("#tabs_related").children().length;

		 

		 $(".shadetabs").find("li").removeClass("selected");

		 $(this).addClass("selected");	 

	

		 $("#tabs_sameartist").hide();

		 $("#tabs_other").hide();

		 

		 if (vid.length > 0 && divs == 0)

		 {

		 	ajax_request('detail', 'do=show_more_related&vid='+ vid, '#tabs_related', 'html', false);

		 }

		 

		 if($.browser.msie) {

			$("#tabs_related").show();

		 }

		 else

		 {

			$("#tabs_related").fadeIn(200);

		 }

		 return false;

	});



	$("#tabs_select_sameartist").click( function() {

		 

		 var vid = $(this).find('a').attr('class');

		 var divs = $("#tabs_sameartist").children().length;

		 

		 $(".shadetabs").find("li").removeClass("selected");

		 $(this).addClass("selected");

		 

		 $("#tabs_related").hide();

		 $("#tabs_other").hide();

		 

		 if (vid.length > 0 && divs == 0)

		 {

		 	ajax_request('detail', 'do=show_more_artist&vid='+ vid, '#tabs_sameartist', 'html', false);

		 }

		 

		 if($.browser.msie) {

			 $("#tabs_sameartist").show();

		 }

		 else {

			 $("#tabs_sameartist").fadeIn(200);

		 }

		 return false;

	});



	$("#tabs_select_other").click( function() {



		 var vid = $(this).find('a').attr('class');

		 var divs = $("#tabs_other").children().length;

		 

 		 $(".shadetabs").find("li").removeClass("selected");

		 $(this).addClass("selected");

		 

		 if (vid.length > 0 && divs == 0)

		 {

		 	ajax_request('detail', 'do=show_more_best&vid='+ vid, '#tabs_other', 'html', false);

		 }

		 

		 $("#tabs_related").hide();

		 $("#tabs_sameartist").hide();

		 if($.browser.msie) {

			 $("#tabs_other").show();

		 }

		 else { 

			 $("#tabs_other").fadeIn(200);

		 }

		 return false;

	});

	

	$("#href_artist_name").click( function() {

		

		 var vid = $(this).attr('class');

		 var divs = $("#tabs_sameartist").children().length;

		 

		 $(".shadetabs").find("li").removeClass("selected");

		 $("#tabs_select_sameartist").addClass("selected");

		 

		 $("#tabs_related").hide();

		 $("#tabs_other").hide();

		 

		 if (vid.length > 0 && divs == 0)

		 {

		 	ajax_request('detail', 'do=show_more_artist&vid='+ vid, '#tabs_sameartist', 'html', false);

		 }

		 

		 if($.browser.msie) {

			 $("#tabs_sameartist").show();

		 }

		 else {

			 $("#tabs_sameartist").fadeIn(200);

		 }

		 return false;

	});

});




