/* -------------------------------------------
  set to false to disable the header rotation
---------------------------------------------- */
var enableHeaderRotate = true;




//add new header bg images as necessary (remember to increment the array index by one for each new image). Also
//remember that the first bg image displayed is actualy slideshow[1], not slideshow[0]
var slideShow = new Array();
slideShow[0] = "images/bodyBG3.jpg";
slideShow[1] = "images/bodyBG.jpg";
slideShow[2] = "images/bodyBG2.jpg";


function fadeinSlideshow(elem, imageList, slideDuration, fadeSpeed, current) {
	
    // get the length of the image array.
    var listSize = imageList.length;   
	// If there's no current image selected, or the value is out of the range of the
    // slideshow, then set the current image to zero.
    if (!current || current >= listSize) current = 0;
	
    // If there's no slide duration set, set it to 5 seconds.
    if (!slideDuration) slideDuration = 15000;
    // If there's no fade speed set, set it to 1 second.
    if (!fadeSpeed) fadeSpeed = 1000;
    // Set the image's source to the current image's url.
    $(elem).attr("src", imageList[current]);
    // If the current element is at the maximum of the element size, then set the 
    // wrapper's background (aka, the next image) to the first image.
    if (current == (listSize - 1)) {
        $(elem).css("background", "#f4f4ed url(" + imageList[0] + ") no-repeat top center");
			
    } else {
    // If not, set the next image in the list to the background of the wrapper.
        $(elem).css("background", "#f4f4ed url(" + imageList[current + 1] + ") no-repeat top center");				
    }
	
    // Hold the current image for a period of time equal to slideDuration. Once that's done, then
    // fade the current image's opacity until the background image shows. Once that is done, then
    // call this same function again with the next image in line.
    $(elem).animate({ opacity: "1" }, slideDuration).animate(
		{ opacity: "1" }, 		
		fadeSpeed, 
		function() { 
			$(this).css("opacity", "1"); 						
			fadeinSlideshow(elem, imageList, slideDuration, fadeSpeed, current + 1);
		});
	
} // end of function fadeinSlideshow()

function runSlideshow(){
    fadeinSlideshow("body", slideShow, 5000, 1000);
}

if(enableHeaderRotate)
//$(document).ready(runSlideshow);
function is_numeric(input){
	return !isNaN(input);
  }
$(document).ready(function(){

	$('.extra_link').hide();

	
	
	var urlHash = window.location.hash.substr(1);
	
	if(is_numeric(urlHash) && urlHash!= '#' && urlHash!= ''){
			
		loadTrack($("#link_"+urlHash));
		
	}
		clickBTN();	


	// tabs 
	$(".tab_content").hide(); //Hide all content
	$("#tabs_btns a:first").addClass("tabActive").show(); //Activate first tab
	$("#parent_title").html($("#tabs_btns a:first").attr("title"));
	$(".tab_content:first").show(); //Show first tab content
	
	$("#tabs_btns a").click(function() {

		$("#parent_title").html($(this).attr("title"));
		$("#tabs_btns a").removeClass("tabActive"); //Remove any "active" class
		$(this).addClass("tabActive"); //Add "active" class to selected tab
		$(".tab_content").hide(); //Hide all tab content
		var activeTab = $(this).attr("rel"); //Find the rel attribute value to identify the active tab + content
		$(activeTab).fadeIn(); //Fade in the active content
		return false;
	});
	// tabs 
	
	
	$('.homeCats').click(function (){
		var $ob = $(this);
		// kill music when load new section 
		
		if(typeof $mplayer !== "undefined" && $mplayer){
		
			$mplayer.jPlayer("stop");
			$mplayer.jPlayer( "clearMedia");
		}
		$(".extra_link").slideUp();
		$("#extra_"+$(this).attr('href').substr(1)).slideDown('fas',function (){
			loadTrack($ob);
		});
		// kill music when load new section 
	});
	
});
var temp_num;

function loadTrack($ob){
		temp_num = null
		//alert($ob);
		var cat_id = $($ob).attr('href');
		var _base = $('#trackContiner').attr('rel');
		var cat_title = "\n<a href=\""+_base+"file/cat/"+cat_id.substr(1)+".html\">"+$($ob).text()+"</a>";
		var cat_desc = $($ob).attr('title');
		var fi;
		cat_desc += "<br /><a href=\""+_base+"/file/cat/"+cat_id.substr(1)+"\">المزيد من الزفات</a>";
		var cat_link = "<a href=\""+_base+"file/cat/"+cat_id.substr(1)+".html\">المزيــد..</a>";
		
		$('#newTracksTitle h2').html(cat_title);
		$('#newTracksTitle p').html(cat_desc);

		$('#trackContiner').html("<p style=\"font-weight:bold\"><img style=\"float:right;border:none\" src=\"./images/loading.gif\" />جاري جلب الزفات</p>");
		$("#trackContiner").load( "./main/home_tracks/"+cat_id.substr(1), { data: '?' }, function() {
			$('#trackContiner').hide();
			$('#trackContiner').fadeIn();
			clickBTN();
			

		});
	
			
		
	};
	function ERR_report(_id,fi)
	{
		
		$('#player_'+_id).after('<p id="error_'+_id+'">نأسف..حصل خطأ اثناء تشغيل الزفة</p>')
		$.post("./report/brokenFile",{item_id:_id, item_name:$('#trackTitle_'+_id).text(),vistor:encode64(fi)} ,function(respose) {
			alert(respose);
		$("#player_"+_id).slideUp('slow',function(){
				
				$("#error_"+_id).fadeOut(5000, function (){
					$(this).detach();
				});
			});
		});
		
		
	}
	


function clickBTN(){
	$("#trackContiner").find('.newMusicButton a').bind({click: function (){
							
						$mplayer = $("#jquery_jplayer_main");
						var playId = $(this).attr('href').substr(6);

						if(playId == temp_num){
							return false;
						}						

						$('#clear_'+playId).html('<p class="waiting_"><img src="./images/loader.gif" style="float:right;border:none;margin:0 0 0 5px;">يرجى الإنتظار</p>');

						$.post("./file/getUrl",{id:playId} ,function(data) 
						{
							   //alert("Data Loaded: " + data);
							   fi = decode64(data);
							 
							if(temp_num == null)
							{
								
								$("#player_"+playId).slideDown('slow',function(){
									$(this).show();
								});
								$mplayer.jPlayer({
									ready: function () {
										
										$(this).jPlayer(
										"setMedia", { mp3:fi}
										
											).jPlayer("play");
											},
											error: function (event){
												
												switch(event.jPlayer.error.type) {
													case $.jPlayer.error.URL:
															ERR_report(playId,btoa(fi));												  return false;
													break;
													
												  }
											},
											ended: function () {
												$mplayer.jPlayer("play", 0);
											},
											swfPath: "./js/",
											solution: "flash, html",
											supplied: "m4a, oga, mp3"
										})
										$mplayer.jPlayer({cssSelectorAncestor: "#jp_interface_"+playId})
										temp_num = playId;
										$('#clear_'+playId).html('');
										return false;
								
							}
							else if(temp_num == playId)
							{
								
								$('#clear_'+playId).html('');
								return false;
							}
							else
							{	
									$mplayer.jPlayer("stop");
									$mplayer.jPlayer( "clearMedia");
									$mplayer.jPlayer({cssSelectorAncestor: "#jp_interface_"+playId})
									$("#player_"+temp_num).slideUp(function (){
									$("#player_"+playId).slideDown('slow');
									});
									
									$mplayer.jPlayer("setMedia", {mp3:fi}).jPlayer("play");
									temp_num = playId;
									$('#clear_'+playId).html('');
									return false;
					
							}
							// error
							
														
						}); // end for POST sucsess method
						
						return false;
				}});


					if(typeof cat_link !== "undefined" && cat_link)
					{
						$('#trackContiner').append("<p class=\"albumText\" style=\"float: right;margin: 0 20px 0 0;top: 2px;width: auto;font-weight:bold;\">"+cat_link+"</p>");
					}
}
