function initPopups() {
  $("a.open-popup").each(function () {
    var link    = $(this);
    var target  = $($(link).attr('href'));

    if($(target).hasClass('signup-required-for-download')) {
      var dimension_options = {
        width:"458px",
        height:$(target).height() + "5"
      }
    } else if ($(target).hasClass('licenses-popup')) {
      var dimension_options = {
        width:"458px",
        height:$(target).height() + "5"
      }
    } else if ($(target).hasClass('premium-terms-popup')) {
      var dimension_options = {
        width:"700px",
        height:$(target).height() + "5"
      }
    } else if ($(target).find('.popup1').length > 0) {
      var height_dimension = ($(target).find('.popup1').height() + 100) + "px"
      var width_dimension  = ($(target).find('.popup1').width()  + 40) + "px"

      var dimension_options = {
        height: height_dimension,
        width:  width_dimension
      }
    }

    var options = {
			transition:'none',
      inline:true,
      opacity:'0.7',
      scalePhotos: true,
      href:$(this).attr('href')
    }

    $.extend(options, dimension_options)

    $(link).click(function () {
      $(target).show();
    })
    $(link).colorbox(options, function () {
      if($(target).hasClass('signup-required-for-download')) {
        $(target).css('margin-top', '40px')
      }
      if($(target).hasClass('licenses-popup')) {
        $(target).css('margin-top', '80px')
      }
      $(".close").click(function () {
        $(target).hide();
        $.fn.colorbox.close();
        return false;
      })
    });
  });
}

function initClickButtons() {
 $('.btns a, .btn-search a, .btn-tiny-blue a, .btn-add a, .btn-preview a').mousedown(function(){
   $(this).addClass('onclick');
 }).mouseup(function(){
   $(this).removeClass('onclick');
 }).mouseleave(function(){
   $(this).removeClass('onclick');
 });
}

function initTabs()
{
 var sets = document.getElementsByTagName("ul");
 for (var i = 0; i < sets.length; i++)
 {
   if (sets[i].className.indexOf("tabset") != -1)
   {
     var tabs = [];
     var links = sets[i].getElementsByTagName("a");
     for (var j = 0; j < links.length; j++)
     {
       if (links[j].className.indexOf("tab") != -1)
       {
         tabs.push(links[j]);
         links[j].tabs = tabs;
         var c = document.getElementById(links[j].href.substr(links[j].href.indexOf("#") + 1));

         //reset all tabs on start
         if (c) if (links[j].className.indexOf("active") != -1) c.style.display = "block";
         else c.style.display = "none";

         links[j].onclick = function ()
         {
           var c = document.getElementById(this.href.substr(this.href.indexOf("#") + 1));
           if (c)
           {
             //reset all tabs before change
             for (var i = 0; i < this.tabs.length; i++)
             {
               var tab = document.getElementById(this.tabs[i].href.substr(this.tabs[i].href.indexOf("#") + 1));
               if (tab)
               {
                 tab.style.display = "none";
               }
               this.tabs[i].className = this.tabs[i].className.replace("active", "");
             }
             this.className += " active";
             c.style.display = "block";
             return false;
           }
         }
       }
     }
   }
 }
}

function toggleCheckboxes(obj) {
    if (!document.getElementById){ return; }
    if (!document.getElementsByTagName){ return; }
    var inputs = obj.getElementsByTagName("input");
    for(var x=0; x < inputs.length; x++) {
        if (inputs[x].type == 'checkbox'){
            inputs[x].checked = !inputs[x].checked;
        }
    }
}

function clearIf(obj, str)
{
 if( obj.value == str )
 {
   obj.value = '';
   $(obj).removeClass('default');
   return false;
 }
 return true;
}

function setClassIf(obj, str, klass)
{
 if( obj.value == str )
 {
   $(obj).addClass(klass);
 }
 else
 {
   $(obj).removeClass(klass);
 }
 return true;
}

function showRatingDescription(rating)
{
 var rating_notes = ['Rate this!', 'Poor', 'Just OK', 'Good', 'Very Good', 'Great!'];
 if( rating_notes[rating] == null ) return;
 $('#rating-notes').hide();
 $('#rating-description').html(rating_notes[rating]).show();
}

function hideRatingDescription()
{
 $('#rating-description').hide();
 $('#rating-notes').show();
}

function rated()
{
 $('.rating-row').removeClass('unrated');
 //$('#rating-notes').html('Thanks!');
}

function reloadPage() {
  window.location = window.location;
}

function setLnb()
{
 var date = new Date();
 var expires = new Date(date.getFullYear()+1, date.getMonth(), date.getDay());
 var str = "lnb="+escape(date.toUTCString())+"; path=/;expires="+expires.toUTCString();
 document.cookie = str;
}

function initCollapsible() {
  $('.collapse-block').each(function() {
    var cblock = $(this);
    if (!(cblock.hasClass('nocollapse'))) {
      cblock.hide();
    }
  });
  $('.collapse-trigger').click(function() {
    var ref = $(this).attr('ref');
    $('#'+ref).toggle();
    return false;
  });
}

$.fn.tracker = function(url) {
  this.each(function () {    
    $(this).click(function () {
      var this_url = url,
          rand = Math.floor(Math.random()*999999);
      
      if( this_url.match(/\?/) ) {
        this_url += '&' + rand;
      } else {
        this_url += '?' + rand;
      }

      if( $(this).attr('href') ) {
        this_url += '&url=' + escape($(this).attr('href'));
      }
      
      new Image().src = this_url;

      return true;
    });
  });
};

//////////////

$(document).ready(function($) {
	initPopups();
	initClickButtons();
	setTimeout("$('.flash-notice, .flash-error').slideUp()", 3000);
	initTabs();
  $(".popupwindow").popupwindow();

  initCollapsible();


  $('.featured-users li:nth-child(3n+1)').addClass('first');
  $('.featured-users li:nth-child(3n+3)').addClass('last');

  $("form").bind("keyup", function(e){
    if(e.keyCode == 13 && $( e.originalTarget ).is( ':input:not(textarea)' )) {
      $(this).submit();
      return false;
    }
  });
  
  $('#toggle_cancel_subscription').click(function () {
    $('#cancel_subscription').show();
    $(this).hide();
    return false;
  });
  
  $('ul.accordion li a.toggle').click(function(e) {
    var toggle = $(this);
    var parent = toggle.parent('li');
    if (parent.hasClass('active')) {
      parent.removeClass('active');
    } else {
      $(parent).addClass('active');
    }
    return false;
  });

});

