function validateFormSearch(form) {
	if (form.search.value == "")
		return false;
}

var lastclick;
function openPLayer(){
  var currentclick = new Date().getTime();
  var fclastclick = lastclick;
  lastclick = new Date().getTime();

  if (fclastclick == null || (currentclick - fclastclick > 1000)) {

    if (typeof(popup_title) == 'undefined' || popup_title == '') {
      popup_title = 'Radio Chic | Chic - Glória Kalil'
    }
    var html = "";
    html += "<html><head><title>" + popup_title + "</title>";
  
    html += "</head><body>";
    html += (document.getElementById("pchic").innerHTML);
    html += "</body></html>";

    if (typeof(xopen) == 'undefined' || xopen.closed == true) {
      xopen = window.open("about:blank","PlayerChic","width=650,height=225,scrollbars=NO,resize=NO");
      xopen.document.write(html);
    } else {
      xopen.focus();
    }
  } else {
    if (typeof(xopen) != 'undefined') {
      xopen.focus();
    }
  }
}

function onReturn(){ return 1; }

var events;
var brands;

var galleries = [];

function mycarousel_initCallback(carroussel){
	var div_parent = carroussel.list.parents("div .tab_gallery");
	galleries.push({obj:carroussel, actualPhoto:1, id:"#"+$(div_parent).attr("id")});
}

function bindArrows(id, actual, tab){

	var actualGallery = {};	
	for(i=0;i<galleries.length;i++){
		if(galleries[i].id == id){
			actualGallery = galleries[i];
		}
	}
	
	actualGallery.actualPhoto = actual;
	var carroussel = actualGallery.obj;
	
	var left_arrow = $(id).parent().parent().find(".left_arrow");
	var right_arrow = $(id).parent().parent().find(".right_arrow");
	
	if(tab){
		$(actualGallery.id+" .jcarousel-list").css("left", 0);
	}
	
	if(carroussel.size() == 1){
		$(left_arrow).hide();
		$(right_arrow).hide();		
	}
	
	$(left_arrow).unbind("click");
	$(right_arrow).unbind("click");
	
	$(left_arrow).bind("click", function() {
		if(actualGallery.actualPhoto%6 == 0){
			carroussel.prev();
		}
		actualGallery.actualPhoto--;
		actualGallery.actualPhoto = (actualGallery.actualPhoto < 1) ? 1 : actualGallery.actualPhoto;
		carroussel.get(actualGallery.actualPhoto).trigger("click");
	});
	$(right_arrow).bind("click", function() {
		if(actualGallery.actualPhoto%6 == 0){
			carroussel.next();
		}
		actualGallery.actualPhoto++;
		actualGallery.actualPhoto = (actualGallery.actualPhoto > carroussel.size()) ? carroussel.size() : actualGallery.actualPhoto;
		carroussel.get(actualGallery.actualPhoto).trigger("click");			
	});	
}

function populateBrands(brands) {
  var options = $.map(brands, function(e) { return '<option value="' + e.slug + '">' + e.name + '</option>'; }).join('');
  $('#search_brand_slug_eq').html('<option value="">Todas as marcas</option>' + options);
  if ($('#brand_slug_eq').size() > 0) {
    $('#brand_slug_eq').html(options);
  }
}

function populateEvents(events) {
  var options = $.map(events, function(e) { return '<option value="' + e.slug + '">' + e.name + '</option>'; }).join('');
  $('#search_events_event_name_slug_eq').html('<option value="">Todos os eventos</option>' + options);
  if ($('#events_event_name_slug_eq').size() > 0) {
    $('#events_event_name_slug_eq').html(options);
  }
}

$(document).ready(function(){

  $(".phone").mask("(99) 9999-9999", { placeholder: " " });

  /* --- EDITANDO PERFIL --- */
  $(".closed").hide();
  var opened = $("#profile_edit_menu .selected");
  $("#profile_edit_menu ul li a").click(function(){
    $(opened).removeClass("selected");
    $($(opened).attr("rel")).hide();
    opened = this;
    $(opened).addClass("selected");
    $($(this).attr("rel")).show();
  });

  $("#networks li").click(function(){
    $(this).find(".status").removeClass("negative");
    if($($(this).find(".painel")).css("display") == "none"){
      $(this).find(".status").addClass("negative");
    }
    $(this).find(".painel").toggle();
  });
  $("#networks li").trigger("click");
  /* --- FIM DA EDICAO DE PERFIL --- */


	/* --- FUNÇÃO DE ABERTURA E FECHAMENTO DO SUB-MENU DO TOPO --- */
	$("#btn_submenu").toggle(function(){
		$(this).addClass('dynamic_color_btn');
		$(this).html("-");
		$('#submenu_more').slideDown();
	}, function() {		
		$(this).removeClass('dynamic_color_btn');
		$(this).html("+");
		$('#submenu_more').slideUp();
	});
	$("#trigger_specials").toggle(function(){
		$("#btn_submenu").addClass('dynamic_color_btn');
		$("#btn_submenu").html("-");
		$('#submenu_more').slideDown();
	}, function() {		
		$("#btn_submenu").removeClass('dynamic_color_btn');
		$("#btn_submenu").html("+");
		$('#submenu_more').slideUp();
	});
	/* --- FIM --- */
	
	/*EXPECIFICO PARA ARTIGOS*/
	//OCULTA E MOSTRA O FORM #FORM_SENDEMAIL
	$(".sendemail").toggle(function(){
		$(this).parent().parent().siblings('.send_by_email').slideDown();
	}, function() {
		$(this).parent().parent().siblings('.send_by_email').slideUp();
	});
	/*FIM DO JS DE ARTIGOS*/

  //OCULTA E MOSTRA A LISTA DE ENQUETES
  $(".poll_list").toggle(function(){
    $(this).parent().next('.poll_result').slideDown();
  }, function() {
    $(this).parent().next('.poll_result').slideUp();
  });

	/* VERIFICA RESOLUÇAO E OCULTA O SKYCRAPPER SE FOR MUITO PEQUENO ---- */
	//var visiblewidth = $(document).width();
	//verifica se a area visivel do navegador é maior que o height do site
	//if(visiblewidth < 1320){
		//Centraliza verticalmente o site inteiro
		//$('#skycrapper').hide();
	//}
	/* FIM DO SKYCRAPPER ---- */

	/* --- FUNÇÃO GENÉRICA DE TROCA DE IMAGENS DOS DESTAQUES --- */
	$('div.myHighlights ul li').click(function(){
		var element = "div#" + $(this).closest('div').attr('id');
		$(element+' ul li.selected').removeClass('selected');
		$(this).addClass('selected');
		$(element+' span#image_big img').hide();
		$(element+' span#image_big span.legend').hide();
		$(element+' span#image_big span.legend').html($(this).find('img').attr('title'));
		$(element+' span#image_big img').load(function(){
			$(element+' span#image_big img').fadeIn("fast");
			$(element+' span#image_big span.legend').fadeIn("normal");
		}).attr('src', $(this).find('img').attr('ref'));
    $(element+' span#image_big a').attr('href', $(this).find('img').attr('link'));
	});
	/* --- FIM --- */

	/* --- FUNÇÃO GENÉRICA DE TROCA DE IMAGENS DOS DESTAQUES --- */
	$('div.myTabs .tab_content').hide();
	$('div.myTabs ul.tabs li').click(function(){
		var element = "div#" + $(this).closest('div').attr('id');
		$(element+' ul.tabs li').removeClass('active');
		$(this).addClass('active');
		$(element+' .tab_content').hide();
		var activeTab = $(this).find('a').attr('href');
		$(activeTab).show();
		return false;
	});
	$('div.myTabs ul.tabs').find('li:first').trigger('click');
	$('div#tab_news ul.tabs').find('li:last').trigger('click'); // Exceção para esta TAB porque tem "float: right"
	/* --- FIM --- */
	
	/* --- FUNÇÃO GENÉRICA DE TROCA DE IMAGENS DO CAROUSEL --- */
	$('.jcarousel-skin-carousel').jcarousel({
		initCallback: mycarousel_initCallback
	});
	$('.jcarousel-skin-carousel li').click(function(){
		var element = "ul#" + $(this).closest('ul').attr('id');
    var gallery_container = $(this).parents('div.images_gallery');
    var current_image = $(this).find('img');
		$(element+' li.selected').removeClass('selected');
		$(this).addClass('selected');
		
		bindArrows("#"+$($(this).parents('div .tab_gallery')).attr("id"), $(this).attr("jcarouselindex"), false);
		
    gallery_container.find('span.legend').html($(this).find('img').attr('legend'));
    gallery_container.find('span.author').html($(this).find('img').attr('author'));
    //gallery_container.find('.media_container').children().find('a').attr('href', '#');
		gallery_container.find('.media_container').children().find('#zoom').attr('big', $(current_image).attr('ref'));
    gallery_container.find('.media_container').children().find('img').attr('src', $(current_image).attr('gallery'));
    gallery_container.find('.media_container').children().find('.mousetrap').remove();
		$('.cloud-zoom, .cloud-zoom-gallery').CloudZoom();
  });
	$('.jcarousel-skin-carousel').find('li:first').addClass('selected');
	/* --- FIM --- */

  /* --- FUNÇÔES DA GALERIA DE IMAGENS --- */
  $('div.myTabs > div.tab_gallery').hide();
  $('div.myTabs').find('div.tab_gallery:first').show();
  $('div.image_tab ul.tabs li').click(function() {
    $(this).parents('.images_gallery').find('div.myTabs > div').hide();
    $(this).siblings().removeClass('active');
    $(this).addClass('active');
    var activeTab = $(this).find('a').attr('href');
		bindArrows(activeTab, 1, true);
    if ($(activeTab).find('ul').children().size() > 1) {
      $(activeTab).show();
    }
    $(activeTab).find('ul li:first').trigger('click');
    return false;
  });
  /* --- FIM --- */

  /* --- PAGINAÇÃO DE COMENTARIOS COM AJAX --- */
  $('div.article_comments div.pagination a').live('click', function() {
    $('#comment_area').load($(this).attr('href'));
    return false;
  });
  $('.video_comments_area .pagination a').live('click', function() {
    $.get($(this).attr('href'));
    return false;
  });
  /* --- FIM --- */

  //$('ul.tabs li:first').trigger('click');
	$('div.myTabs ul.tabs').find('li:first').trigger('click');
  
  $('#box_login').load('/person_sessions');

  $('#select_states').live('change', function(e) {
    $.getJSON('/states/' + $(this).val() + '/cities', function(data) {
      $('#select_cities').children().remove();
      $.each(data, function(i, item) { $('#select_cities').append('<option value="' + item.city.id + '">' + item.city.name + '</option>'); });
    });
  });

  $('#search_season_slug_eq, #season_slug_eq').change(function(e) {
    $('#search_brand_slug_eq, #brand_slug_eq').html('<option value="">Carregando...</option>');
    $('#search_events_event_name_slug_eq, #events_event_name_slug_eq').html('<option value="">Carregando...</option>');
    $.getJSON('/events', { 'season': $(this).val() }, function(data) {
      populateEvents(data.events);
      populateBrands(data.brands);
    });
  });

  $('#search_events_event_name_slug_eq, #events_event_name_slug_eq').change(function(e) {
    var the_season = $('#season_slug_eq').val();
    if (the_season == "" || the_season == null) {
      the_season = $('#search_season_slug_eq').val();
    }
    $('#search_brand_slug_eq, #brand_slug_eq').html('<option value="">Carregando...</option>');
    $.getJSON('/events', { 'event': $(this).val(), 'season': the_season }, function(data) {
      populateBrands(data.brands);
    });
  });

  $('ul.imb li:last').trigger('click');

  $('#profile_box_networks').accordion({
    fillSpace: true,
    autoHeight: false,
    collapsible: true
  });
	
	setTimeout("$.each($('div.image_tab ul.tabs'), function(i, item) { $(item).children('li:first').trigger('click'); });", 1400);

  $('div#news ul.tabs').find('li:last').trigger('click'); // Exceção para esta TAB porque tem "float: right"

  $('#search_season_slug_eq, #season_slug_eq').trigger('change');

});

