/*JQUERY START*/
$(function(){
    
    //ContentFlow
    if($("#contentFlow").length){
      var cf = new ContentFlow('contentFlow' , {reflectionHeight:0.1 , reflectionGap: 0.05, relativeItemPosition: "center center" ,maxItemHeight: 120});
    }
    
    $('.icoPanel a').click(function(){
	var $vlr = $(this).attr('class');
	var cf = $(this).parent().next();
	var gv = $(this).parent().next().next();
	$('.icoPanel a').removeClass('atv');
	switch ($vlr){
	case "btoFlow":
	  $(this).addClass('btoFlowAtv');
	  $(this).next().removeClass('btoGridAtv');
	  gv.hide();
	  cf.show();
	  break;
	case "btoGrid":
	  $(this).prev().removeClass('btoFlowAtv');
	  $(this).addClass('btoGridAtv');
	  cf.hide();
	  gv.show();
	  break;
	}
      });

    $('.flow .item, .lstGrid li a:not(.nocolorbox)').colorbox({previous: 'anterior',
	  next: 'próximo',
	  close: 'CERRAR',
	  current : 'Imágen {current} de {total}',
	  loop: false,
	  onComplete:function(){ //manipula links de redes sociais
	  var imgNum = $('#cboxItmNum').text();
	  var link = $('a[rev="' + imgNum + '"]').next().next();
	  if ($('#cboxDownload').length) {
	    $('#cboxDownload').html('<a target="_blank" class="cboxDownload" href="' + link.attr('href') + '">Download</a>');
	  } else {
	    $('#cboxShare').before('<div id="cboxDownload"><a target="_blank" class="cboxDownload" href="' + link.attr('href') + '">Download</a></div>');
	  }
	}
      });
    $('.flow a[rev="'+gup('opMod')+'"]').click();
    
    //ColorBox
    $("a.colorbox-trigger, .event-gallery a").colorbox({close:'cerrar ventana', next:'Siguiente', previous:'Anterior', current:'Imagen {current} de {total}'});

    //CALENDAR
    $("#calendar form").submit(function(e){e.preventDefault();return false;});
    $("#calendar .prevbtn").click(function(){
        var currentmargin = $(".scrollable2").css("marginLeft");
        var sizescreen = 200*1;
        var sizescreen2 = 190*1;
        if($(".scrollable2:animated").length == 0){// prevent the 'buggy' animation
          if(currentmargin != '0px'){
            $(".scrollable").animate({marginLeft:'+='+sizescreen},250,'easeInCubic');
            $(".scrollable2").animate({marginLeft:'+='+sizescreen2},250,'easeInCubic');
          }
        }
	return false;
      });
    $("#calendar .nextbtn").live("click",function(){
        var currentmargin = $(".scrollable2").css("marginLeft");
        var sizescreen = 200*1;
        var sizescreen2 = 190*1;
        if($(".scrollable2:animated").length == 0){// prevent the 'buggy' animation
          if(currentmargin != '-4180px'){
            $(".scrollable").animate({marginLeft:'-='+sizescreen},500,'easeInCubic');
            $(".scrollable2").animate({marginLeft:'-='+sizescreen2},500,'easeInCubic');
          }
        }
        return false;
      });
    
    $("#calendar-q").keyup(function () {
        var filter = $(this).val(), count = 0;
        $(".column li p").each(function () {
            if ($(this).text().search(new RegExp(filter, "i")) < 0) {
              $(this).parent().parent().hide();
            } else {
              $(this).parent().parent().slideDown();
              //count++;
            }
          });
        //$("#filter-count").text(count);
      });

    $("#filter-chihuahua").click(function(){
	if($("#filter-chihuahua").is(':checked')){
	  $("#filter-auditorio-municipal").parent().show();
	  $("#filter-palomar").parent().show();
	  $("#filter-plaza-mayor").parent().show();
	  $("#filter-teatro-de-camara").parent().show();
	  $("#filter-teatro-de-la-ciudad").parent().show();
	  $("#filter-teatro-de-los-heroes").parent().show();
	  $("#calendar-q").val("");
	  $("#filter-auditorio-municipal, #filter-palomar, #filter-plaza-mayor, #filter-teatro-de-camara, #filter-teatro-de-la-ciudad, #filter-teatro-de-los-heroes").attr("checked","checked");
	  $('[idcity=1]').slideDown(600);
	}else{
	  $("#filter-auditorio-municipal").parent().hide();
	  $("#filter-palomar").parent().hide();
	  $("#filter-plaza-mayor").parent().hide();
	  $("#filter-teatro-de-camara").parent().hide();
	  $("#filter-teatro-de-la-ciudad").parent().hide();
	  $("#filter-teatro-de-los-heroes").parent().hide();
	  $("#calendar-q").val("");
	  $('[idcity=1]').slideUp(600);
	}
      });
    $("#filter-juarez").click(function(){
	if($("#filter-juarez").is(':checked')){
	  $("#filter-carpa-ccpn").parent().show();
	  $("#filter-explanada-ccpn").parent().show();
	  $("#filter-teatro-vhrb").parent().show();
	  $("#calendar-q").val("");
	  $("#filter-carpa-ccpn, #filter-explanada-ccpn, #filter-teatro-vhrb").attr("checked","checked");
	  $('[idcity=2]').slideDown(600);
	}else{
	  $("#filter-carpa-ccpn").parent().hide();
	  $("#filter-explanada-ccpn").parent().hide();
	  $("#filter-teatro-vhrb").parent().hide();
	  $("#calendar-q").val("");
	  $('[idcity=2]').slideUp(600);
	}
      });
    $("#filter-parral").click(function(){
	if($("#filter-parral").is(':checked')){
	  $("#filter-carpa-parral").parent().show();
	  $("#filter-foro-villista").parent().show();
	  $("#filter-teatro-doc").parent().show();
	  $("#calendar-q").val("");
	  $("#filter-carpa-parral, #filter-foro-villista, #filter-teatro-doc").attr("checked","checked");
	  $('[idcity=3]').slideDown(600);
	}else{
	  $("#filter-carpa-parral").parent().hide();
	  $("#filter-foro-villista").parent().hide();
	  $("#filter-teatro-doc").parent().hide();
	  $("#calendar-q").val("");
	  $('[idcity=3]').slideUp(600);
	}
      });
    
    $("#filter-auditorio-municipal").click(function(){if($("#filter-auditorio-municipal").is(':checked')){$('[idlocation=3]').slideDown(600);}else{$('[idlocation=3]').slideUp(600);}});
    $("#filter-carpa-ccpn").click(function(){if($("#filter-carpa-ccpn").is(':checked')){$('[idlocation=9]').slideDown(600);}else{$('[idlocation=9]').slideUp(600);}});
    $("#filter-explanada-ccpn").click(function(){if($("#filter-explanada-ccpn").is(':checked')){$('[idlocation=8]').slideDown(600);}else{$('[idlocation=8]').slideUp(600);}});
    $("#filter-palomar").click(function(){if($("#filter-palomar").is(':checked')){$('[idlocation=6]').slideDown(600);}else{$('[idlocation=6]').slideUp(600);}});
    $("#filter-plaza-mayor").click(function(){if($("#filter-plaza-mayor").is(':checked')){$('[idlocation=1]').slideDown(600);}else{$('[idlocation=1]').slideUp(600);}});
    $("#filter-teatro-de-camara").click(function(){if($("#filter-teatro-de-camara").is(':checked')){$('[idlocation=5]').slideDown(600);}else{$('[idlocation=5]').slideUp(600);}});
    $("#filter-teatro-de-la-ciudad").click(function(){if($("#filter-teatro-de-la-ciudad").is(':checked')){$('[idlocation=4]').slideDown(600);}else{$('[idlocation=4]').slideUp(600);}});
    $("#filter-teatro-de-los-heroes").click(function(){if($("#filter-teatro-de-los-heroes").is(':checked')){$('[idlocation=2]').slideDown(600);}else{$('[idlocation=2]').slideUp(600);}});
    $("#filter-teatro-vhrb").click(function(){if($("#filter-teatro-vhrb").is(':checked')){$('[idlocation=7]').slideDown(600);}else{$('[idlocation=7]').slideUp(600);}});
    $("#filter-carpa-parral").click(function(){if($("#filter-carpa-parral").is(':checked')){$('[idlocation=10]').slideDown(600);}else{$('[idlocation=10]').slideUp(600);}});
    $("#filter-foro-villista").click(function(){if($("#filter-foro-villista").is(':checked')){$('[idlocation=12]').slideDown(600);}else{$('[idlocation=12]').slideUp(600);}});
    $("#filter-teatro-doc").click(function(){if($("#filter-teatro-doc").is(':checked')){$('[idlocation=11]').slideDown(600);}else{$('[idlocation=11]').slideUp(600);}});


    populateElement("#search-q","BUSCAR");
    populateElement("#name","Nombre");
    populateElement("#telefono","Teléfono");
    populateElement("#correo","Correo");
    populateElement("#mensaje","Mensaje");
    
    //ROUNDABOUT
    $('ul#myRoundabout').roundabout({minOpacity: 0, minScale: 0.6, maxZ: 50, minZ: 0});
    //$(".roundabout-in-focus img").live("click",function(){var myUrl = $(this).attr("rel");document.location.href=myUrl;return false;});

    //YTPLAYLIST
    $("ul.demo2").ytplaylist({addThumbs:true, autoPlay: false, holderId: 'ytvideo2'});

    //MENU
    $("#nav li").hoverIntent(function(){
	$(this).find(".tab").show();
      },function(){
	$(this).find(".tab").hide();
      });

    //PARALLAX EFFECT ---------------------------------------------------------------------------
    var deviceAgent = navigator.userAgent.toLowerCase();
    var agentID = deviceAgent.match(/(iphone|ipod|ipad)/);
    if (agentID) {
      // do something special
    }else{
      $(".l-outer-third-overlay").scrollingParallax({staticSpeed : .4, staticScrollLimit : false});
      $(".l-outer-fifth-overlay").scrollingParallax({staticSpeed : .2, staticScrollLimit : false});
    }
    //end: PARALLAX EFFECT ---------------------------------------------------------------------

    //Contacto
    $("#contactform form").submit(function(e){
	e.preventDefault();
	var name = $("#name").val();
	var correo = $("#correo").val();
	var telefono = $("#telefono").val();
	var mensaje = $("#mensaje").val();
	if (correo === null || correo == "Correo" || ! correo.match(/^[\w\-\+\._]+\@[a-zA-Z0-9][-a-zA-Z0-9\.]*\.[a-zA-Z]+$/)) {
	  alert("Debes introducir una direccion de correo valida");
	}else if(name === null || name === "" || name == "Nombre"){
	  alert("Teclea tu nombre completo");
	}else if(mensaje === null || mensaje === "" || mensaje == "Mensaje"){
	  alert("Teclea tus comentarios");
	}else{
	  $("#name, #correo, #telefono, #mensaje").val("");
	  $.get("ajaxcontact.pl",{name:name, correo:correo, mensaje:mensaje, telefono:telefono},function(data){alert(data);});
	}
      });

    //Load videos from user on youtube
    if($("#youtube").length>0){
      $.ajax({type: "GET", url: "http://gdata.youtube.com/feeds/users/fich2011/uploads?alt=json-in-script&format=5",//generateJSonPlayListCallBackUrl(),
	    cache: false, dataType:'jsonp', success: function(data){showMyVideos(data);$("#ytvideolist ul").hoverscroll({vertical:true,width:370,height:380,arrows:false});}});
    }
    
    //trigger calendar
    $("#calendario-trigger-down").click(function(){
	$(this).slideUp("slow",function(){
	    $("#calendario-trigger-up").slideDown();
	    $("#calendar-trigger-container").slideDown("slow");
	  });
      });
    $("#calendario-trigger-up").click(function(){
	$("#calendar-trigger-container").slideUp("slow",function(){
	    $("#calendario-trigger-up").slideUp();
	    $("#calendario-trigger-down").slideDown();
	  });
      });
    
  });
/* JQUERY END */

/*FUNCTIONS*/
function showMyVideos(data) {var feed = data.feed;var entries = feed.entry || [];var html = ['<ul class="videos">'];for (var i = 0; i < entries.length; i++){var entry = entries[i];var title = entry.title.$t;var thumbnailUrl = entries[i].media$group.media$thumbnail[0].url;var playerUrl = entries[i].media$group.media$content[0].url;html.push('<li onclick="loadVideo(\'', playerUrl, '\', true)">','<b>', title, '</b><img src="',thumbnailUrl, '" width="120" height="90"/>', '</span></li>');}html.push('</ul><br style="clear: left;"/>');document.getElementById('ytvideolist').innerHTML = html.join('');if (entries.length > 0) {loadVideo(entries[0].media$group.media$content[0].url, false);}}
function loadVideo(playerUrl, autoplay) {swfobject.embedSWF(playerUrl + '&rel=1&border=0&fs=1&autoplay=' + (autoplay?1:0), 'ytvideoplayer', '380', '600', '9.0.0', false, false, {allowfullscreen: 'true'});}

function populateElement(selector, defvalue) {$(selector).each(function() {if($.trim(this.value) == "") {this.value = defvalue;}});$(selector).focus(function() {if(this.value == defvalue) {this.value = "";}});$(selector).blur(function() {if($.trim(this.value) == "") {this.value = defvalue;}});}

function gup(name){ // captura parametro na url
  var name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&]*)";var regex = new RegExp( regexS );var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

