function formSubmit(btn) {
  $("#login-form").validate();
  $(btn).parents("form").submit();
  return false;
}
var loading_html = "<div style='text-align: center'><img src='static/images/ajax-loader.gif' alt='Loading...' align='middle' /><p class='highlight1'>Please wait, while response is retrieved.</p> </div>";

$(document).ready(function(){



  if (navigator.userAgent.match(/iPad/i) != null) {
    $('body').removeClass('no-touch');
    clickevent='touchend';
  }
  else clickevent='click';

  //sidebar accordion menu
  jQuery('#home-sidebar').accordion({ 
    header: '.title',
    active: false,
    alwaysOpen: false, 
    autoHeight: false,
    event:clickevent,
    collapsible: true,
    icons: false
  });


  //bookmark fading
  $( 'a.love1, a.love, a.love2').live("click", function(e) {
    e.preventDefault();
    var  object_id =  $(this).data("id");
    var url = "bookmark/" +  object_id;
    var clicked_obj = $(this);
    //  console.log($(this));
    $.post(url,
           function(data){
             if (data=="0") {
               //   alert("The video has been removed from bookmark");   
               clicked_obj.fadeTo("fast", .5);
             } else if (data=="1") {
               // alert("You have bookmarked the video");   
               clicked_obj.fadeTo("fast", 1);
             }
           });
    return false;
  });

  //create tabs function
    $("#video-tabs > ul a").bind(clickevent, function() {
        $("#video-tabs > ul li.current").removeClass("current");
        $(this).parent("li").addClass("current");
        $("#video-tabs > div > div").removeClass("current");
        $("#video-tabs > div > div:eq("+$('#video-tabs > ul a').index(this)+")").addClass("current");
        $("#video-tabs #vimeo iframe").remove();
        $("#video-tabs > div > div.current #vimeo").append(vimeo_code);
        return false;
    });

//  open the login light box
    $('.loginbox').click(function (e) {
        $($(this).attr("href")).modal({
            maxHeight:$(window).height()-60, 
            opacity: 80, 
            onClose: function(){
                $(".tooltips").hide();
                $.modal.close();
            }
        });
        return false;
    });

  //learn about plans links
  $('#plan-terms a.learn-subscribe').click(function () {
    if ($($(this).attr("href")).is(":visible")) {
      $($(this).attr("href")).slideUp();      
      $($(this).attr("href")).siblings(".hidden_info").slideDown();
      $("#plan-terms a span").html("&minus;");
      $(this).children("span").html("");
    }
    else {
      $(this).parent().siblings(".hidden_info").slideUp();
      $($(this).attr("href")).slideDown();
      $("#plan-terms a span").html("");
      $(this).children("span").html("&minus;");
    }
    return false;
  });


  //set the height of the tabs to that of the tallest tab
  var tabheight = 0;
  $("#video-tabs > div > div").each(function(){
    if(tabheight < $(this).height())
      tabheight = $(this).height();
  });
  $("#video-tabs > div > div").height(tabheight);

  //show the first tab
  $("#video-tabs > ul li:eq(0)").addClass("current");
  $("#video-tabs > div > div:eq(0)").addClass("current");

  
  vimeo_code=$("#video-tabs #vimeo").html();
  $("#video-tabs #vimeo iframe").remove();

  //more info links
  $('.more_info a').click(function () {
    if ($($(this).attr("href")).is(":visible"))
    {
      $($(this).attr("href")).slideUp();
      $('.more_info a').children("span").html("+");
    }
    else
    {
      $(this).parent().siblings(".hidden_info").hide();
      $($(this).attr("href")).slideDown();
      $('.more_info a').children("span").html("+");
      $(this).children("span").html("&minus;");
    }
    return false;
  });

  //learn about plans links
  $('#plan-terms a.learn-subscribe').click(function () {
    if ($($(this).attr("href")).is(":visible"))	{
      $($(this).attr("href")).slideUp();		
      $($(this).attr("href")).siblings(".hidden_info").slideDown();
      $("#plan-terms a span").html("&minus;");
      $(this).children("span").html("+");
    }
    else {
      $(this).parent().siblings(".hidden_info").slideUp();
      $($(this).attr("href")).slideDown();
      $("#plan-terms a span").html("+");
      $(this).children("span").html("&minus;");
    }
    return false;
  });

  //public video library link to video page 
  $('#pubcontent .entry-pubcontent').click(function () { 
    document.location=$(this).find("a:eq(0)").attr("href"); 
  });

  //profile photo pan
  $("#profile-photo img").panFullSize(292,292);

  //tooltip
  if (clickevent=='touchend')
  {
    $("a.tooltip-trigger").click(function(event){
      tip=$($(this).attr("href"));
      tip.toggle();
      if (tip.is(":visible")) $(document).click(function(){$(".tooltips").hide();$(document).unbind("click");});
      tip.css("top",event.pageY-tip.outerHeight()-15).css("left",event.pageX-20);
      return false;
    });
  }
  else {
    $("a.tooltip-trigger").click(function(event){
      return false;
    });
    $("a.tooltip-trigger").mouseenter(function(event){
      tip=$($(this).attr("href"));
      tip.show();
      tip.css("top",event.pageY-tip.outerHeight()-15).css("left",event.pageX-20);
    });
    $("a.tooltip-trigger").mouseleave(function(event){
      tip=$($(this).attr("href"));
      tip.hide();
    });
  }

  //edit one field
  $('a.edit-btn').click(function () {
    $(this).parents(".editbox").find(".current").hide();
    $(this).parents(".editbox").find("a.btn").slideDown();
    $(this).parents(".editbox").find(".hidden").show();
   // alert($(this).parents(".editbox").find(".hidden").val());
    return false;
  });
  
  //edit one section
  $('a.section-edit-btn').click(function () {
    $(this).parents(".section").find(".current").hide();
    $(this).parents(".section").find("a.btn").slideDown();
    $(this).parents(".section").find(".hidden").not(".editbox").show();
    return false;
  });

// changing the credit card data from profile and billing page
$("#cc_change").live("click", function(e){
  e.preventDefault();
  $("#creditcard-lb").modal({
                maxHeight:$(window).height()-60, 
                // top, left
                position:[5, 250],
                opacity: 80, 
                onClose: function(){
                    $(".tooltips").hide();
                    $.modal.close();
                }
            });
});

//uploading the profile pic to the server
$("#upload_pic").live("click", function(e){
  e.preventDefault();
  alert("here");
  
});

//for channels html page
$('#channels-sidebar').accordion({ 
        header: '.title',
        active: 0,
        alwaysOpen: true, 
        autoheight: false,
        event:clickevent
    });

 $('#channels-sidebar .title').bind(clickevent, function() {
        if ($("#content > ul.plans:eq("+$('#channels-sidebar .title').index(this)+")").is(":visible"))
            return;
        $("#content > ul.plans").hide();
        $("#content > ul.plans:eq("+$('#channels-sidebar .title').index(this)+")").fadeIn();
    });

//save button click for edit on field
$('a.save-field-data').click(function (e) {
  var text_val = $(this).parents(".editbox").find(".hidden").val();
  $(this).prevAll("p.current").text(text_val);
    e.preventDefault();
    var url = "profile_management/"
    db_field = $(this).data("id");
    $(this).parents(".editbox").find(".hidden").hide();
    $(this).parents(".editbox").find(".current").show();
    $.post(url,{
      field:db_field,
      val: text_val
    });
    
    return true;
  });


  $('p.addlink a').click(function () {
    if (!$(this).parent().prev(".editbox").is(":visible"))
    {
      $(this).parent().prev(".editbox").slideDown();
    }
    return false;
  });

  // Scroll globals
  var pageNum = 1; // The latest page loaded
  var hasNextPage = true; // Indicates whether to expect another page after this one
  var baseUrl = '/video_pagination_json/'; // The root for the JSON calls

  var loadItems = function(e) {
      e.preventDefault();
      // If the next page doesn't exist, just quit now 
      if (hasNextPage === false) {
	  return false
      }
      // Update the page number
      pageNum = pageNum + 1;
      // Configure the url we're about to hit
      var url = baseUrl + pageNum + '/';
      $.ajax({
	  url: url, 
	  success: function(data) {
	      // Update global next page variable
	      //hasNextPage = data.hasNext;
	      // Loop through all items
	      // Pop all our items out into the page
              console.log(data);
	      $("#video-anchor").before(data);
	  },
        error: function(data) {
            //console.log(data);
            $("#id_load_more").addClass("more-vids no-more");
            $("#id_load_more").text("No More Videos");
        }
      });
  };

  $("#id_load_more").click(loadItems);

  
});



