


/* Cookie reference
$.cookie('the_cookie'); // get cookie
$.cookie('the_cookie', 'the_value'); // set cookie
$.cookie('the_cookie', 'the_value', { expires: 7 }); // set cookie with an expiration date seven days in the future
$.cookie('the_cookie', '', { expires: -1 }); // delete cookie
*/

/* Global Variables */
selectedview = 'list';



$(document).ready(function(){
						   
  // Add link to Synopsis then hide until used
  $('#center .channel .body').after('<a href="#" class="synopsislink">Synopsis</a>');
  $('.synopsislink').hide();
  
  

  dropDownMenuSetup();

  addShadows();
 
  shareSetup();

  activateListeners();

  listenToChannels();






  // Sort
  $('#popup .sort li a').click(function () {
		sortId = $(this).parent().attr('id');							 

		$('#center-content .view-content:first').hide();
		sortchannels(sortId);
		$('#popsortlink').text($(this).text());
		$('#popsortlink').attr('class', sortId + ' active');
		
		activateListeners();
		listenToChannels();
		
        $(this).blur();
	    return false;
  });
  

  
  
  
  
  if( $.cookie('view') == 'large' ) {
    $('#popviewlink').text('Large');
	$('#popviewlink').attr('class', 'largelink');
	if ( $(".view-content .channel").length > 0 ) {
	  $('#center-content .view-content').hide();
	  showLargeThumbs();
	}
  } else if( $.cookie('view') == 'small' ) {
	$('#popviewlink').text('Small');
	$('#popviewlink').attr('class', 'smalllink');
	if ( $(".view-content .channel").length > 0 ) {
      $('#center-content .view-content').hide();
	  showSmallThumbs();
	}
  }





  // change View
  $('#popup .view li#listlink a').click(function () {
    $.cookie('view', 'list', { expires: 31, path: '/' } );
    $('#center-content .view-content').fadeOut('fast', showList);
	$('#popviewlink').text($(this).text());
	$('#popviewlink').attr('class', $(this).parent().attr('id') + ' active');
    $(this).blur();
    return false;
  });  
  $('#popup .view li#largelink a').click(function () {
    $.cookie('view', 'large', { expires: 31, path: '/' });
    $('#center-content .view-content').fadeOut('fast', showLargeThumbs);
	$('#popviewlink').text($(this).text());
	$('#popviewlink').attr('class', $(this).parent().attr('id') + ' active');
    $(this).blur();
    return false;
  }); 
  $('#popup .view li#smalllink a').click(function () {
    $.cookie('view', 'small', { expires: 31, path: '/' });
    $('#center-content .view-content').fadeOut('fast', showSmallThumbs);
	$('#popviewlink').text($(this).text());
	$('#popviewlink').attr('class', $(this).parent().attr('id') + ' active');
    $(this).blur();
    return false;
  });
  
  

  
  


  // Popup Suggest window
  $('.suggest-link').click( function() {
	$('#suggest').fadeIn('fast');
	$('#vale').show();
	return false;
  });
  $('#suggest .close').click( function() {
	$('#suggest').fadeOut('fast');
  	$('#vale').hide();
	return false;
  });

});





  function showList() {
      $('.channel-rating').show();
      $('.body').show();
      $('.channel-links').show();
      $('.synopsislink').hide();
	  
	  if(selectedview == 'small') {
  // Convert all 'Channel' images to 'Thumbnail'
    $('#center .image-attach-teaser img').each(function() {
	  $(this).attr('width', '300');
      $(this).attr('height', '200');
	  $(this).parent().parent().attr('style', '');
    });
	  }
												
		$('#center').removeClass();
        selectedview = 'list';
		
    $('#center-content .view-content').fadeIn('fast');  
  }
  function showLargeThumbs() {
	  $('.view .channel-rating').hide();
      $('.view .body').hide();
      $('.view .channel-links').hide();
      $('.view .synopsislink').hide();
	  
	  if(selectedview == 'small') {
  // Convert all 'Channel' images to 'Thumbnail'
    $('#center .image-attach-teaser img').each(function() {
	  $(this).attr('width', '300');
      $(this).attr('height', '200');
	  $(this).parent().parent().attr('style', '');
    });
	  }
	    $('#center').removeClass();
		$('#center').addClass('large');
        selectedview = 'large';
    $('#center-content .view-content').fadeIn('fast');	  
  }
  function showSmallThumbs() {
  // Convert all 'Channel' images to 'Thumbnail'
    $('#center .image-attach-teaser img').each(function() {
	  $(this).attr('width', '150');
      $(this).attr('height', '100');
	  $(this).parent().parent().attr('style', '');
    });
	
	$('.view .channel-rating').hide();
    $('.view .view .body').hide();
    $('.view .synopsislink').hide();
    $('.view .channel-links').hide();
	
	$('#center').removeClass();
	$('#center').addClass('small');
    selectedview = 'small';
    $('#center-content .view-content').fadeIn('fast');
  }




function findSettings() {
  if($('#center').hasClass('list')) { selectedview = 'list'; }
  if($('#center').hasClass('large')) { selectedview = 'large'; }
  if($('#center').hasClass('small')) { selectedview = 'small'; }
}



function listenToChannels() {
	
  findSettings();	
	
  // Large and Small Thumbs mousover
  $('#center .view div.channel').mouseover(function() {
    if(selectedview == 'large' || selectedview == 'small'){
      $(this).find('.channel-rating').show();
      $(this).find('.body').show();
      $(this).find('.channel-links').show();
	  if(selectedview == 'small') {
        $(this).find('.synopsislink').show();
	  }
	}
  }).mouseout(function() {
    if(selectedview == 'large' || selectedview == 'small'){
      $('.view .channel-rating').hide();
      $('.view .body').hide();
      $('.view .channel-links').hide();
      $('.view .synopsislink').hide();
	}
  });
 
}




function dropDownMenuSetup() {
  // DROP DOWN MENU
  // show popup - sort
  $('#popsortlink').click(function() {
    $('#popup .sort').show();
    $('#popup .view').hide();
	$('#vale').show();
    $('#popup').slideDown('slow');
	
	$('#popsortlink').addClass('active');
	
    this.blur();
	return false;
  });
  // show popup - view
  $('#popviewlink').click(function() {
    $('#popup .sort').hide();
    $('#popup .view').show();
	$('#vale').show();
    $('#popup').slideDown('slow');
	
	$('#popviewlink').addClass('active');
	
    this.blur();
	return false;
  });
  // hide popup
  $('.closelink').click(function() {
    $('#popup').slideUp('slow');
	//$('body').css('overflow','auto');
	$('#popsortlink, #popviewlink').removeClass('active');
	$('#vale').hide();
	return false;
  });

  $('#vale').click(function() {
	$(this).hide();
	$('#popup').slideUp('slow');
	$('body').css('overflow','auto');
	$('#share').fadeOut('fast');
	$('#synopsis').fadeOut('fast');
	$('#suggest').fadeOut('fast');
	$('#popsortlink, #popviewlink').removeClass('active');
  });

   // Fade Popup links
   $("#popup a").animate({ 
     opacity: 0.5
   }, 10 );

    $("#popup a").hover(
      function () {
        $(this).animate({ 
          opacity: 1.0
        }, 100 );
      }, 
      function () {
        $(this).animate({ 
          opacity: 0.5
        }, 500 );
    });
}




function addShadows() {
  // Links Shadow or light - double text
   $('#menu li a, #siteslogan').each(function() {
    $(this).html('<span class="shadow">' + $(this).text() + '</span> <span class="original">' + $(this).text() + '</span>');
   });
   
   $('.visit a strong, .suggest-link').each(function() {
    $(this).html('<span class="light">' + $(this).text() + '</span> <span class="original">' + $(this).text() + '</span>');
   });
}






function shareSetup() {
  // Share links
  /*
  $('.channel-links').each(function() {
		channelTitle = $(this).parent().find('h2').text();						
		$(this).prepend('<li class="share"><a href="#"><strong><span class="light">Share</span> <span class="original">Share</span></strong><span class="title"> ' + channelTitle + '</span></a></li>');
  });
  */
  

    //Share popup
  $('.share a').click( function() {
								
	$('#share iframe').attr('src', $(this).attr('href'));
	
	//populate
	channelTitle = $(this).parent().parent().parent().find('h2').text();
	channelNodeUrl = $(this).parent().parent().parent().find('h2 a').attr('href');
	
	$('#share h2').html('<strong><span class="shadow">Share</span> <span class="original">Share</span></strong><span class="title"> ' + channelTitle  + '</span>');
	
	$('#share .pageURL input').attr('value', channelNodeUrl);
	

	
	$('#share li a').each( function() {
      url = $(this).attr('href');
      url = url.replace("urlerror", channelNodeUrl);
      url = url.replace("titleerror", channelTitle);
      $(this).attr('href', url);
    });
								
	$('#share .highlight').removeClass('highlight');							
								
	$('#share').fadeIn('fast');
	
	$('#vale').show();
	
    this.blur();
	return false;
  });
  
  
  //Share close
  $('#share .close').click( function() {
	$('#share').fadeOut('fast');
	$('#vale').hide();
	return false;
  });	
}








  
  
  
  
  
function activateListeners() {
	  
	// test start
  // Large and Small Thumbs mousover
  listenToChannels();
  
  // Re-activate Fivester
  if($('div.fivestar-form-item:first').length > 0) {
    $('div.fivestar-form-item').rating();
    $('input.fivestar-submit').css('display', 'none');
	// remove additional stars added each time
	$('div.fivestar-widget:last-child').remove();
  }
  
  // Popup Synopsis window (Small thumbs)
  $('#center .channel .synopsislink').click(function() {
	output = '<h2>' + $(this).parent().parent().find('h2').text() + '</h2>';
	output += '<div id="synopsis-text">' + $(this).parent().find('.body').html() + '</div>';

	$('#synopsis-inner').html(output);
	$('#synopsis').fadeIn('fast');
	
	$('#vale').show();
	
	this.blur();
	return false;
  });
  // Popup Synopsis close
  $('#synopsis .close, #synopsis .closebutton').click( function() {
	$('#synopsis').fadeOut('fast');
	$('#vale').hide();
	return false;
  });




  //Share popup 
  $('.share a').click( function() {
					
	//populate
	channelTitle = $(this).parent().parent().parent().find('h2').text();
	channelNodeUrl = 'http://www.channelsonline.tv' + $(this).parent().parent().parent().find('h2 a').attr('href');
	
	$('#share h2').html('<strong><span class="shadow">Share</span> <span class="original">Share</span></strong><span class="title"> ' + channelTitle  + '</span>');
	
	$('#share .pageURL input').attr('value', channelNodeUrl );
	
	$('#share li a').each( function() {
      url = $(this).attr('href');
      url = url.replace("urlerror", channelNodeUrl);
      url = url.replace("titleerror", channelTitle);
      $(this).attr('href', url);
    });
								
	$('#share .highlight').removeClass('highlight');							
								
	$('#share').fadeIn('fast');
	
	$('#vale').show();
	
    this.blur();
	return false;
  });
// test end  
	  
}
  
  
  
  
  
  
  
  
// because I'm lazy latest is done using node id
function sortchannels(sortId) {
	 howToSort = sortId;
	 
	 // if A-Z copy the title to the beginning
     if(howToSort == 'azlink' || howToSort == 'zalink') {
	   $('#center-content div.channel').each(function() {
			$(this).prepend( '<span class="tempinfo">' + $(this).find('h2').text() + '</span>' );
		});
	 }
	 
	 // if Rating copy adverage rating to the beginning
     if(howToSort == 'toplink' || howToSort == 'lowlink') {
	   $('#center div.channel').each(function() {
			voteAverage = $(this).find("input[name='vote_average']").attr('value');
			if (voteAverage==undefined) {
              voteAverage = '00';
            }
			if (voteAverage=='100') {
              voteAverage = '99';
            }
			voteAverage = voteAverage.substr(0,2);
			$(this).prepend( '<span class="tempinfo">' + voteAverage + '</span>' );
		});
	 }
	 
	 
	 var myarray = [];
     $("#center-content .view-content:first div.channel").each(function() {
		 myarray.push($(this).html()) 
	  });
	 
     myarray.sort();
	 
	 if(howToSort == 'zalink' || howToSort == 'latestlink' || howToSort == 'toplink' ) {
		 myarray.reverse();
	 }
	 
	 var output = "";
	 for(i=0; i < myarray.length; i++) {
	   output += '<div class="channel">' + myarray[i] + '</div>';
	 }
	 $('#center-content .view-content:first').html(output);
	 
	 // remove A-Z sort info  before fadingIn
	 $('span.tempinfo').remove();
	 
     $('#center-content .view-content:first').fadeIn('slow');
	 
}