$(document).ready(function() {
	// Stuff to do as soon as the DOM is ready;
	
	var commentListingSize = $(".commentListing").size()
	for(x=0;x<commentListingSize;x++){
		var commentSize = $(".commentListing").eq(x).find(".comment").size()
		
		$(".commentListing").eq(x).find(".comment").eq(0).css("display", "block")
		$(".commentListing").eq(x).find(".comment").eq(1).css("display", "block")
		commentSize = $(".commentListing").eq(x).find(".comment").size()
		//alert(commentSize)
		if(commentSize >3){
			commentSize -= 3
			$(".commentListing").eq(x).find(".comment").eq(1).after("<a href='#' class='seeAllComments'>Il y a "+commentSize+" autres commentaire</a>")
		}
		var lastComment = commentSize -1
		$(".commentListing").eq(x).find(".comment").eq(lastComment).css("display", "block")
		
	}
	var commentHashOpen = location.hash
	if(commentHashOpen){
		var patt=new RegExp("_comments");
		if(patt.test(commentHashOpen)){
			commentHashOpen = commentHashOpen.replace("_comments","")
			$(commentHashOpen).find(".commentListing").css("display","block")
		}
		commentHashOpen = location.hash
		
		if(commentHashOpen ==  "#demandes_speciales"){
			$("#firstname").focus()
			$("#demande").animate({backgroundColor:"#f6fd31"},"slow").animate({backgroundColor:"#ffffff"},3000)
		}
	}
	var cellVal = $("#nocell").val()
	if(cellVal== "--"){
		$("#nocell").val("ex:514-433-7712")
		$("#nocell").click(function() {
			// Act on the event
			$(this).val("")
		});
	}
	
	$(".moreOnDemand").click(function() {
		// Act on the event
		$(this).parent().parent().find(".resulting").css('display','block')
		$(this).css('display','none')
		return false;
	});
	
	
	/*
	var searchLength = $(".resulting").size()

	if(searchLength<5){
		startBlock = "#search"+searchLength
				$(".radioDemandeResults dd").css("display","block")
				$(".radioDemandeResults dt").css("display","block")
				$(".radioDemandeResults dl").css("display","block")
				$(".radioDemandeResults .resulting").css("display","block")
				$(".voirPLus").css("display","none")
	}else{
		startBlock = "#search6"
			$(".voirPLus").css("display","block")
			$(".voirPLus").click(function() {
				// Act on the event
				$(".resulting").css("display","block")
				$(".radioDemandeResults dd").css("display","block")
				$(".radioDemandeResults dt").css("display","block")
				$(".radioDemandeResults dl").css("display","block")
				return false;
			});
	}
	$(startBlock).parent().parent().css("display","block")
	$(startBlock).prevAll().css("display","block")
	$(startBlock).parent().parent().prevAll().css("display","block")
	$(startBlock).parent().parent().parent().css("display","block")
	$(startBlock).parent().parent().parent().prevAll().css("display","block")
	
	*/
	$(".newss .body:first").css("display","block")
	$(".newss a.titleNews").click(function() {
		// Act on the event
		$(this).parent().parent().find(".body").slideToggle("slow");
		return false;
	});
	$(".blogEntries a.commentListLink").click(function() {
		// Act on the event
		$(this).parent().parent().parent().find(".commentListing").slideToggle("slow");
		return false;
	});
	
	// Radio a la demande on change
	 $("#byShow").change( function() {
	 	 url = $("#byShow option:selected").val();	
	 	 location.href=url
	 })	 	

	$("#stationSelection").dialog({
		autoOpen  : false,
		modal     : true,
		width: 390,
		title     : 'Choisis ta station',
		resizable : false 
	});
	$("#voting").dialog({
		autoOpen  : false,
		modal     : true,
		title     : 'Merci pour ton vote',
		resizable : false
	});
	
	$("a.stationSelect").click(function() {
		// Act on the event
		$("#stationSelection").dialog("open");
		return false;
	});
	// SHOW START COMMENTS
	
	
	// SHOW COMMENTS
	$("a.seeAllComments").live("click",function() {
		// Act on the event
		$(this).css('display','none')
		$(this).parent().find(".comment").slideDown()
		return false;
	});
	
	/* INPUT CONNECT FOCUS */
	
	$("#loginBtn").click(function() {
		// Act on the event
		$("input#email").select()
	});
	
	
	
	/* youtube video insert, select all the field on click */
	$("input#legacyId").click(function()
	 {
	  // only select if the text has not changed
	 	 if(this.value == this.defaultValue){this.select(); }
	 })
	 /* YOUTUBE DELETE VALUES */
	 
	 $("#deleteValues").click(function() {
	 	if($("input#deleteValues:checked")[0]){

	 		titleVideo = $("input#title").val()
	 		descVideo = $('#Description___Frame').contents().find('#xEditingArea iframe').contents().find("body").html()
	 		$("input#title").val(" ")
			$('#Description___Frame').contents().find('#xEditingArea iframe').contents().find("body").html(" ")
	 		$(".imagesVideo").css("display","none")
	 		
	 	}else{
	 		$("input#title").val(titleVideo)
			$(".imagesVideo").css("display","block")
			$('#Description___Frame').contents().find('#xEditingArea iframe').contents().find("body").html(descVideo)
	 	}
	 	// Act on the event
	 });
	 
	 
	 
	 /* SCHOW SCHEDULE PAGE WITH THE GOOD DAY */
	
	 	 $("select#showSchedule").change( function() {
	 
		  		var scheduleday = $("#showSchedule option:selected").val();	
		  		location.href = scheduleday
	  		 
	  		return false;
		});
	 	
	
	 /* SHOW PALMARES RIGHT BOX*/
	
	 	 $("#decompteDropDown").change( function() {
	 	
	 	 	changeOption = $("#decompteDropDown option:selected").val();	
	
		  	 $(".decompteList").css("display","none")
			  		
			  		$(changeOption).css("display","block")
			 

	  		return false;
		});
		
		 $("#topdownloadDropdown").change( function() {
	 	
	 	 	changeOption = $("#topdownloadDropdown option:selected").val();	
	
		  	 $(".decompteListTel").animate({opacity:0},function(){
			  		$(".decompteListTel").css("display","none")
			  		$(changeOption).css("display","block")
			  		$(changeOption).animate({opacity:1})
			  })

	  		return false;
		});
		
		
		
		
	/* NOUS JOINDRE SHOW ANIMATEUR */
	
			changeAnim = $(".contactDeptSelect option:selected").val();	
			if(changeAnim == "animateur"){
				$("#hostId").animate({opacity:1})
			}else{
				$("#hostId").animate({opacity:0})
			}
	
	 $(".contactDeptSelect").change( function() {
	 	
	 	 	changeAnim = $(".contactDeptSelect option:selected").val();	
	 	 
			if(changeAnim == "animateur"){
				$("#hostId").css('display','block')
				$("#hostId").animate({opacity:1})
			}else{
				$("#hostId").animate({opacity:0},function(){
					$("#hostId").css('display','none')
				})
			}

	  		return false;
		});



		/* VOTING SYSTEMS 
		$('.voting').click(function() {
		
				postUrl = location.href
				caller = this
				songIDvalue = $(this).attr("rel")
				decompteIDvalue = $(this).attr("title")
				 $.ajax({
				   type: "POST",
				   url: postUrl,
				   data: "songID="+songIDvalue+"&decompteID="+decompteIDvalue,
				   success: function(){
				   		$("#voting").dialog("open")
				    	var voteNumber = $(caller).parent().parent().find(".voteNumber").text()
				    	voteNumber = parseFloat(voteNumber)
				    	voteNumber += 1
				    	
				    	$(caller).parent().parent().find(".voteNumber").text(voteNumber)
				    	$("#closeVote").live("click", function() {
				    		// Act on the event
				    		$("#voting").dialog("close")
				    	});
				    	
				   }
				});
			return false;
		})
*/
			
});

	function formValid(){
		$("#newsletterForm").submit(function() {
			// Act on the event
			if($('#horoscope').is(':checked')){
				if($("#horos option:selected").val() ==""){
					erreur=true;
					$("#errorHoros").remove()
					$("#horos").before("<div id='errorHoros' style='color:red;'>Veuillez choisir un signe</div>")
			
				}else{
					erreur = false;
					$("#errorHoros").remove()
				}
			}
			if($('#circulation').is(':checked')){
				if($("#startTime option:selected").val() =="-1"){
					$("#errorCircu").remove()
					$("#startTime").before("<div id='errorCircu' style='color:red;'>Veuillez choisir une heure pour chaque champs</div>")
					erreur=true;
				}else{
					$("#errorCircu").remove()
					if(!erreur){
						erreur=false;
					}
				}
			}
			if(erreur){
				return false;
			}
		});
	}
	function moveLang(){
		var commentHashOpen = location.href
		if(commentHashOpen){
			var patt=new RegExp("langue/en");
			if(patt.test(commentHashOpen)){
				$(".tabNavigation").find("li").eq(1).find("a").trigger("click")
			}
		}
	}
	function nousjoindre(){
		var commentHashOpen = location.hash
		if(commentHashOpen){
			var patt=new RegExp("#liste");
			if(patt.test(commentHashOpen)){
				$(".tabNavigation").find("li").eq(1).find("a").trigger("click")
			}
		}
	}
	Global = {
		FixPng: function( img ){
			if(document.all){
				img.parentNode.style.width = img.offsetWidth;
				img.style.visibility = "hidden"
				img.parentNode.style.height = img.offsetHeight;
				img.parentNode.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale src='"+ img.src +"')"
			} else {
				img.style.visibility = "visible"
			}
		}
	}
	var movedItem = false
	function hitlistvote(){
		
		$("a.canVote").click(function() {
			// Act on the event
			showBanner();
			caller = this
			songIDpost = parseFloat($(this).attr("rel"))
			movedItem = false
			decompteIDpost = parseFloat($(this).attr("title"))
		 	omnitureTrack('Vote Hit Liste - ' + $(this).attr("rev"));
			region = $(this).attr("id")
			$.post('/'+region+'/ajax/voteondecompte',{songID: songIDpost, decompteID: decompteIDpost},
			 function(){
			 	$("#voting").dialog("open")
			    //stuff to do *after* page is loaded;
			    $("#closeVote").live("click", function() {
				    		// Act on the event
				    		$("#voting").dialog("close")
				    	});
			});
			
			
			callerParent = $(this).parent().parent().parent().parent().parent()
			callerParent.find(".arrow").removeClass("arrowDown")
			callerParent.find(".arrow").addClass("arrowUp")
			var currentvote = parseFloat(callerParent.attr("rel")) +1
			
			
			$(caller).parent().parent().find(".voteNumber").html( currentvote)
				
			$(caller).parent().parent().find(".voteNumber").html( currentvote)
			if(callerParent[0] != $("li.voteRes")[0]){		// DO NOT MOVE ARROWS IF FIRST ONE
			$("li.voteRes").each(function(index) {
				
				if(movedItem == false){	
					testVoteNode = $(this)
				
					testVote = parseFloat(testVoteNode.attr("rel"))
					if(testVoteNode[0] != callerParent[0]){
						if(currentvote > testVote){
							movedItem = true
							callerParent.css("display","none")
							$(this).before(callerParent)
							callerParent.find(".arrow").removeClass("arrowDown")
							callerParent.find(".arrow").addClass("arrowUp")
							callerParent.css("display","block")
							callerParent.nextAll("li.voteRes").each(function(index) {
								$(this).find(".arrow").addClass("arrowDown")
								$(this).find(".arrow").removeClass("arrowUp")
							})
							$("li.voteRes").each(function(index) {
								position = index +1
								$(this).find(".spot").html("N<sup>o</sup>"+position)
							})
						}
					}
					
				
				}	
			});
				}
			callerParent.attr("rel", currentvote)
			return false;
		});
	}
	function xmlRingtones() {	
		$.get('/js/jquery/xmlSonnerie.php', function(xml){
			var xmlobject = (new DOMParser()).parseFromString(xml, 'text/xml');
			
			$(xmlobject).find('ringtone').each(function(){
	
				title = $(this).find("title").text()
				
				
				artist = $(this).find("artist").text()
				 var sonnerie = '<li>\
					            	<div class="left">\
					                	<div class="spot">N<sup>o</sup><?php echo $k + 1 ?></div>\
					                </div>\
					                <dl>\
					                    <dt>'+artist+'</dt>\
					                   <dd>'+title+'</dd>\
					                    <dd class="links">\
					                        <ul>\
					                            <li><a href="#"><div class="redButtonLeft">�couter</div><span>�couter</span></a></li>\
					                            <li><a href="#"><div class="purchaseIconLeft">Acheter</div><span>Acheter</span></a></li>\
					                        </ul>\
					                    </dd>\
					                </dl>\
					           </li>'
					          
				});
			})
		}

