
function isArray (obj) {
  return obj.constructor == Array;
}

$.fn.markAsInvalid = function () {
  $(this).addClass ('invalid');
}

$.fn.toggleTabActivity = function () {
  switch ($(this).attr ('id')) {
    case 'developmentQueue':
      $(this)
        .attr ('id', 'developmentQueueActive')
        .siblings ('li#orderOnWaitActive').attr ('id', 'orderOnWait');
      break;
    case 'orderOnWait':
      $(this)
        .attr ('id', 'orderOnWaitActive')
        .siblings ('li#developmentQueueActive').attr ('id', 'developmentQueue');
      break;
    default:
      break;
  }
}

$.fn.slideRibbon = function () {
  var heightOpen = 173;
  var heightClosed = 15;
  var animationSpeed = 100;
  var heightNew = heightOpen;
  if ($(this).height () >= heightOpen) {
    heightNew = heightClosed;
  }
  $(this)
    .stop (true, false)
    .animate (
      { height: heightNew },
      animationSpeed,
      function () {
        if ($(this).hasClass ('ribbonOpen')) {
			    $(this)
			      .removeClass ('ribbonOpen')
			      .addClass ('ribbonClosed');
			  }
			  else if ($(this).hasClass ('ribbonClosed')) {
			    $(this)
			      .removeClass ('ribbonClosed')
			      .addClass ('ribbonOpen');
			  }
				$(this)          
          .removeAttr ('style');
		  var exdate=new Date();
		  exdate.setDate(exdate.getDate()+100);
		  if($(this).hasClass("ribbonClosed")) {document.cookie='ribbon=0;expires='+exdate.toGMTString()+'; path=/';}
		  else {document.cookie='ribbon=1;expires='+exdate.toGMTString()+'; path=/';}
      }
    );
}

$.toggleClass = function (class1, class2) {
  if ($(this).hasClass (class1)) {
    $(this)
      .removeClass (class1)
      .addClass (class2);
  }
  else if ($(this).hasClass (class2)) {
    $(this)
      .removeClass (class2)
      .addClass (class1);
  }
}

$(function () {
  /* Text input behaviour
   */
  var textInputs = $('input.text, textarea');
  textInputs
    // Store default values
    .each (function () {
      $(this).data ('default', {value: $(this).val()});
    })
    // Clear element on focus if default value
    .focus (function () {
      if ($(this).val () === $(this).data ('default').value) {
        $(this).val ('');
      }
    })
    // Restore element on blur if empty
    .blur (function () {
      if ($(this).val () === '') {
        $(this).val ($(this).data ('default').value);
      }
    });
    
    /* Development ribbon behaviour
    */
    var developRibbon = $('div#developRibbon')
    if (!$('html').hasClass ('ie')) {
      if (developRibbon.length > 0) {
        var tabStripes = $('div#developRibbon ul.tabs li h2');
        tabStripes.click (function () {
          var id = $(this).parent('ul.tabs li').attr ('id');
          var idActivity = id.substr (id.length - 6, 6);
          if (idActivity == 'Active') {
            developRibbon.slideRibbon ();
          }
          else {
            $(this).parent('ul.tabs li').toggleTabActivity ();
            if (developRibbon.hasClass ('ribbonClosed')) {
              developRibbon.slideRibbon ();
            }
          }
        });
      }
    }
    if (developRibbon.length > 0) {
      // Carousel start
      $('ul#carousel').jcarousel ({
        scroll: 10
      });
    }
    
    /* Sponsors animation
    */
    var sponsorAnimationPause = 1000;
    var sponsorAnimationSpeed = 100;
    var sponsorsContainer = $('div#photoSponsor ul');
    var sponsorLogos = $('li', sponsorsContainer);
    var activeSponsorClass = 'active';
    
    if (sponsorLogos.length > 1) {
      $(sponsorsContainer).cycle('fade');
    }
    
    /* Add new album behaviour
     */
    $('form#albumNewForm').submit (function (e) {
      $('input#albumNewSave').click();
      return false;
    });
    
    // invite friends
    var onFriendInvite = function () {
      $("#friendInviteForm").toggle();
    }
    $("#friendInvite").bind ("click", onFriendInvite);
    var onFriendInviteSave = function () {
      if($("#friendInviteForm textarea").attr("value")!="") {
        $("#friendInviteStatus").html("<img src='/images/loading.gif' alt='' />");
        $.post("/tools/invitefriend/", {emails:$("#friendInviteForm textarea").attr("value")}, function(data) {
          $("#friendInviteStatus").html(""+data);
          $("#friendInviteForm textarea").attr("value","");
          $("#friendInviteForm").hide();
        },"html");
      }
      else {$("#friendInviteStatus").html("Moraš upisati bar jednu email adresu!");}
    }
    $("#friendInviteSave").bind ("click", onFriendInviteSave);
});