$(document).ready(function()
{  
   $(":radio").css(
   {
      border: "none"
   });
     
   scroll_top();
   
   /**
    * Disable some links or inputs
    */
   ingore_disabled();
   
   /**
    * Add target="_blank" to some links
    * Open them in new window
    */
   jQuery('a[@rel^="external"]').attr('target', '_blank');
   
   
   //Playlist popup
   popup_playlist();
   
   //Track downloads with no permissions
   track_nopermission_download();
   
   //Cheack simple search
   simple_search();
   
   //Auto-select
   auto_select_text();
   
   //Fix CSS
   $("input:radio").css({border:"none"});
   $("input:checkbox").css({border:"none"});
   
   $("#loginForm input[name=email]").example('email@domain.com');
   $("input[id=header_query]").example(function() {
      var text = $(this).attr('title');
      $(this).attr('title');
      return text;
   });
   
   $('.tooltip').tooltip({ 
      track: true, 
      delay: 0, 
      showURL: false, 
      showBody: " - ", 
      extraClass: "pretty", 
      fixPNG: true, 
      opacity: 0.95, 
      left: -120 
  });
});



function ingore_disabled()
{
   $('a[@rel^="disabled"]').click(function(event)
   {
      //Stop event
      event.stopPropagation();
      return false;
   });
}

//Check the simple search
function simple_search()
{
   check_simple_search();
   
   $("#header_query").keypress(function()
   {
      check_simple_search();
   });
   
   $("#header_query").blur(function()
   {
      check_simple_search();
   });
}

function check_simple_search()
{
   var search_query = $("#header_query").attr("value");
   
   if (!search_query) 
   {
      var search_query_length = 0;
   }
   else 
   {
      var search_query_length = search_query.length + 1;
   }
   
   if (search_query_length > 3) 
   {
      $("#search_btn_header").removeAttr('disabled');
   }
   else 
   {
      $("#search_btn_header").attr('disabled', 'disabled');
   }
}

/**
 * Track downloads with no permissions
 */
function track_nopermission_download()
{
   //scroll to top of page
   $(".download-nopermission").click(function(event)
   {
      var errormsg = 'You do not have permission to download this item!\nPlese login and try again.';
      errormsg = errormsg.replace(/\n/g, '<br />');
      
      jQuery.facebox('<div class="small_error">' + errormsg + '</div>');
      
      //Stop event
      return false;
   });
}

function scroll_top()
{
   //scroll to top of page
   $(".scroll_top").click(function(event)
   {
      $('html, body, #top').animate(
      {
         scrollTop: 0
      }, 'slow');
      
      //Stop event
      event.stopPropagation();
      return false;
   });
}

/**
 * In-page popup for lessons playlist
 */
function popup_playlist()
{
   $('.pop_playlist').click(function(event)
   {
      //get href attribute
      var url = $(this).attr("href");
      
      //additional parameters
      var params = "";
      
      var stopEvent = true;
      
      //make ajax request and get content
      var html = $.ajax(
      {
         type: "GET",
         url: url,
         data: "ajax=true" + params,
         cache: false,
         async: false,
         success: function(msg)
         {
            jQuery.facebox(msg);
         },
         error: function(msg)
         {
            var stopEvent = false;
         }
      }).responseText;
      
      if (stopEvent) 
      {
         //Stop event
         event.stopPropagation();
         return false;
         //end ajax
      }
      
   });
   //end on-click event
}

function auto_select_text()
{
   $('#copy_search_widget').click(function(event)
   {
      $("textarea#search_widget").select();
      copy_clipboard(document.getElementById('search_widget'));
   });
   $('textarea#search_widget').focus(function(event)
   {
      $(this).select();
      copy_clipboard(document.getElementById('search_widget'));
   });
   $('#copy_search_css_widget').click(function(event)
   {
      $("textarea#search_css_widget").select();
      copy_clipboard(document.getElementById('search_css_widget'));
   });
   $('textarea#search_css_widget').focus(function(event)
   {
      $(this).select();
      copy_clipboard(document.getElementById('search_css_widget'));
   });
}

function copy_clipboard(inElement)
{
   var swf_url = HTTP_PATH + 'flash/clipboard/clipboard.swf';

   if (inElement.createTextRange) 
   {
      var range = inElement.createTextRange();
      
      if (range && BodyLoaded == 1) 
      {
         range.execCommand('Copy');
      }
   }
   else 
   {
      var flashcopier = 'flashcopier';
      
      if (!document.getElementById(flashcopier)) 
      {
         var divholder = document.createElement('div');
         divholder.id = flashcopier;
         document.body.appendChild(divholder);
      }
      document.getElementById(flashcopier).innerHTML = '';
      var divinfo = '<embed src="' + swf_url + '" FlashVars="clipboard=' + encodeURIComponent(inElement.value) + '" width="0" height="0" type="application/x-shockwave-flash"></embed>';
      document.getElementById(flashcopier).innerHTML = divinfo;
   }
}

function is_banned_domain(url)
{
   alert(url);
}
