/* ---------------------------------------------------------------------------------------
   FUNCTION TO VALIDATE MAILING LIST              
   ------------------------------------------------------------------------------------ */

$(document).ready(function() {


       jQuery.fn.correo=function(){
          if(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test($(this).val())){
             return true;
          }else{
             return false;
          }
       }

  $("#Submit").attr('disabled','disabled');
  var ma_u = false;
  var ma_e = false;
  $("#mailing_list_email_image").hide();
  $("#mailing_list_name_image").hide();

//  $("#mailing_list_user_name,#mailing_list_user_email").change(function() {
  $("#mailing_list_user_name,#mailing_list_user_email").bind('change keypress focusout', function(event) {
     if($("#mailing_list_user_email").correo()){
        var ma_e = true;
     }else{
        var ma_e = false;
     }
     if($("#mailing_list_user_name").val() > ' '){

         var ma_u = true;
     }else{
        var ma_u = false;
     }

     if(ma_u == false){
         $("#mailing_list_name_image").show();
     }else{
         $("#mailing_list_name_image").hide();
     }

     if(ma_e == false){
         $("#mailing_list_email_image").show();
     }else{
         $("#mailing_list_email_image").hide();
     }

     if(ma_u == true && ma_e == true){
       $("#Submit").attr('disabled','');
     }
 });


});





$(document).ready(function() {
	// this function give us an accordion like effect. 
	var id = '0';
	var is_open = '0';
	
	$('.podcast_tab').click(function() {
			
			is_open = $(this).attr("id");
			
			if(is_open != id ){
				$('.' + id).slideToggle('slow',function() {});
				id = $(this).attr("id");
				$('.' + id).slideToggle('slow',function() {});
				$('.' + id).attr('COLSPAN', '4');
			}
			
	});
});


//NEWS - Archive TreeView
$(document).ready(function() {
  $("#archive_tree").treeview({
    animated: "300",
		persist: "location",
		collapsed: true,
		unique: true
	}); 
});



$(document).ready(function() {
	$('.add_video_comment').bind('click', function() {
	 	var id = $(this).attr("id");
		$(".comment_form").load('/articles/' + id + '/comment');
		return false
	});
	
	$('.add_photo_comment').bind('click', function() {
	 	var id = $(this).attr("id");
		$(".comment_form").load('/articles/' + id + '/comment');
		return false
	});
	
	
	$('.add_album_comment').bind('click', function() {
	 	var id = $(this).attr("id");
		$(".comment_form").load('/albums/' + id + '/comment');
		return false
	});
	
	$('.add_article_comment').bind('click', function() {
	 	var id = $(this).attr("id");
		$(".comment_form").load('/articles/' + id + '/comment');
		return false
	});
	
	
});


$(document).ready(function() {
	
	$('.view_album').click(function() {
		id = $(this).attr('id');
		
		$('.musicdescription').slideUp();		
		$.get("/albums/" + id, function(data){
			$('.musicdescription').html(data);
			$('.musicdescription').slideDown();
		 });	
	});
});

$(document).ready(function() {
	
	$('.albums_covers').click(function() {
		i = $(this).attr('id');
		$('.musicdescription').slideUp();		
		$.get("/albums/" + i, function(data){
			$('.musicdescription').html(data);
			$('.musicdescription').slideDown();
		 });	
	});
});


$(document).ready(function() {
    $(function() {
      
	//vars
	var conveyor = $(".content-conveyor", $("#sliderContent")),
	item = $(".item", $("#sliderContent"));
	
	//set length of conveyor
	conveyor.css("width", item.length * parseInt(item.css("width")));
			
      //config
      var sliderOpts = {
	  max: (item.length * parseInt(item.css("width"))) - parseInt($(".viewer", $("#sliderContent")).css("width")),
        slide: function(e, ui) { 
          conveyor.css("left", "-" + ui.value + "px");
        }
      };

      //create slider
      $("#slider").slider(sliderOpts);
    });
	
});

$(function() {
	$('#eventscalendar').datepicker({ 
		altField: 'input#date', 
		altFormat: 'D, d M yy',
		onSelect: function(dateText, inst) { document.formulario_search.submit(); },
		onChangeMonthYear: function(year, month, inst) { month_calendar(month,year) }
	});
	$('.resultsPoll').hide();
});




/* ---------------------------------------------------------------------------------------
   FUNCTION TO COLOR THE CALENDAR BY THE EVENTS                   
   ------------------------------------------------------------------------------------ */
function month_calendar(month, year) {
		  
  $.getJSON("/calendar_events?month="+month+"&year="+year, function(data){
    $.each(data, function(i,item){
  
   var presale = false;
      
   var date = new Date();                            // full date of today
   var today = date.getDate();                       // just the day of today
   var eventdate = parseInt(item.date.substring(8)); // the 2 last numbers from each event date

   presale = item.is_presale                   // presale flag
   

	  // searching every row of the calendar
	  $('#eventscalendar .ui-datepicker-inline table td').each(function(){
		
		// getting the text of the <a>
		var eventdateint = parseInt($(this).children().text());
		 
		// compare to the current event date  
		// if the <a> text is equal to the event date apply the showDate class  
		if ( eventdateint == eventdate ){
			// compare to presale value
			// if the presale value is equal to true apply the preSale class
			if ( presale == true  ){
			  $(this).children().addClass("preSale");
			}else{
		          $(this).children().addClass("showDate");
		        }

	    }
		
					
	  });// end each row loop
	  

	});// end each event loop
    
  });// end the json
}      




/* ---------------------------------------------------------------------------------------
   SHARED THIS WIDGET                   
   ------------------------------------------------------------------------------------ */

$(document).ready(function() {
	var shared_object = SHARETHIS.addEntry({
		title: "Check out this from tiesto.com -",
		url: document.location.href
	});
	shared_object.attachButton(document.getElementById("ck_sharethis"));		
	
	$('.username').click(function() {
		return false
	});
});




/* ---------------------------------------------------------------------------------------
   FUNCTION TO POLL              
   ------------------------------------------------------------------------------------ */

function submitPoll(){
	var podcastId = $('#podcast_podcast_id').val();	
	var data = 'podcast[podcast_id]=' + podcastId + '&commit=SUBMIT+VOTE';
	
	$.ajax({  
    type: "GET",  
    url: "/poll/vote",  
    data: data,  
    success: function() {
			$('#pollForm').hide();
			$('.club_life_poll').height(250);
			$('.resultsPoll').load('/poll');
      $('.resultsPoll').show();
			return false;
		}  
  });  /* end ajax */

	return false;	
}


/* ---------------------------------------------------------------------------------------
   FUNCTION TO PREVIEW THE COMMENTS                   
   ------------------------------------------------------------------------------------ */
function previewComment() {
  // remove previous preview comment box
  $('#previewcomment').empty();
	  
  // display the previewCommentBox
  $('.previewCommentBox').css('display', 'block');

  // store the comment temporary
  var comment  = $('#user_comment').val();
  
  // display the comment on the previewCommentBox
  $('#previewcomment').append(comment);
}





