/* ------------------ ON LOAD ------------------ */
$(document).ready( function(){
		// cufon
		Cufon.replace('.content h1', { fontFamily: 'Juice ITC', hover: true });
		
		// main menu drop downs
	  $("#main-menu li").hover( function(){ $('ul:first',this).show(); }, function(){ $('ul:first',this).hide(); } );
	  //$("#menu li ul li:has(ul)").find("a:first").parent().append(" <span>&gt;</span>");
		
		// multimedia files
		$.fn.media.defaults.flvPlayer = '/static/cms/flash/mediaplayer.swf';
		$.fn.media.defaults.mp3Player = '/static/cms/flash/mediaplayer.swf';		
		$(document).ready(function() { $('a.media').media(); });    
		
		// unicorn
		$(window).resize(function() {
				if ($(window).width() > 1100) $('#unicorn').fadeIn();
				else $('#unicorn').fadeOut();					
		});
		if ($(window).width() > 1100) $('#unicorn').fadeIn();
		
		// disable right click		
    $(document).bind("contextmenu",function(e){
		    return false;
    });
});

/* ------------------ AJAX FORM SUBMIT ------------------ */
function ajaxFormSubmit(frm) 
{				
	toggleLoader(frm,true);

	// send request
	$.post($(frm).attr('action'), $(frm).serialize(),
	  function(data){
	   	showResponse(frm,data.msg,data.status);	   	
			toggleLoader(frm,false);
	  }, 
	 "json");	
}

function toggleLoader(frm,on) 
{
	if (on) { $(".submit", frm).hide(); $(".loader", frm).css('display','inline'); }
	else { $(".submit", frm).css('display','inline'); $(".loader", frm).hide(); }
}

function showResponse(frm,msg,status) 
{
	if (status > 0) {
		$("form",$(frm).parent()).slideUp();
		$(".response",$(frm).parent()).hide().removeClass('error').addClass('success').html(msg).slideDown();		
	}
	else {
		$(".response",$(frm).parent()).hide().removeClass('success').addClass('error').html(msg).slideDown();	
	}	
}

/* ---------------- WIDGETS ---------------- */
function widget(name,url) 
{ 
	$("#"+name+"-container").html('<img class="loader" src="/static/images/ajax-loader'+(name=='events' ? '2' : '')+'-m.gif" border="0" width="66" height="66" />');
	$("#"+name+"-container").load(url,function(){$('table',$(this)).fadeIn(); $('a',$(this)).fadeIn(); });
} 

/*
 * $ Ajax Event Tip
 *
 * http://rndnext.blogspot.com/2009/02/$-ajax-tooltip.html
 */; 
$(function()   
{   
  var hideDelay = 500;     
  var currentID;   
  var hideTimer = null;     
  var container = $('<div id="event-tip"></div>');
  
  $('body').append(container);   
  
  $('.event-tip').live('mouseover', function()   
  {   
		var params 	= $(this).attr('rel').split(',');   
		var day 		= params[0];
		var month 	= params[1];
		var year 		= params[2];
		if (day == '' || month == '' || year == '') return;   
			  
		if (hideTimer) clearTimeout(hideTimer);   
  
		var pos 	= $(this).offset();   		
		var wwidth= $(window).width();
		container.css({left: pos.left + 20 + 'px', top: pos.top - 5 + 'px'});   
  
		$('#event-tip').html('<img src="/static/images/ajax-loader2-s.gif" width="16" height="16" border="0" class="loader">');   
  
		$.ajax({   
    	type: 'GET',   
      url: '/ajax/dogodki',
			data: 'day=' + day + '&month=' + month + '&year=' + year,   
      success: function(data)   
      {   
				$('#event-tip').html(data);
			}   
		});   
		container.css('display', 'block');   
  });   
  
  $('.event-tip').live('mouseout', function()   
  {   
		if (hideTimer) clearTimeout(hideTimer);   
		hideTimer = setTimeout(function() { container.css('display', 'none'); }, hideDelay);   
  });   
  
  // Allow mouse over of details without hiding details   
  $('#event-tip').mouseover(function()   
  {   
		if (hideTimer) clearTimeout(hideTimer);   
  });   
  
  // Hide after mouseout   
  $('#event-tip').mouseout(function()   
  {   
		if (hideTimer) clearTimeout(hideTimer);   
		hideTimer = setTimeout(function() { container.css('display', 'none'); }, hideDelay);   
  });   
});

/* ------------------ LOOP ITEMS ------------------ */
(function($)
{
	var LoopItems = function(element,options)
  {
		var iiWrapper 	= $('.item',element);
		var navWrapper  = $('.navigation',element);
						
		var iiCurent 	= 1
		var iiCount 	= iiWrapper.size();

		// Show first item and activate first page navigation
		iiWrapper.filter(':first').show().addClass('cur-itm');
		$('ul li:first',navWrapper).addClass('cur-page');
		
		// Merge options with defaults  
		var settings = $.extend({  
				duration: '9000'
		}, options || {});
				
		// Run slide show
		var iiInterval = setInterval("var loopitems = $('#"+element.attr('id')+"').data('loopitems'); loopitems.loopRun();",settings.duration);						
				
		// Set navigation buttons onclick
		$('ul li a',navWrapper).click(function () 
		{		
			var loopitems = $('#'+$(this).parents('.loop').attr('id')).data('loopitems');
			clearInterval(iiInterval);
			loopitems.loopGoTo(this);
			return false;
		});		   	

    this.loopAnimate = function(nex)
    {
			iiWrapper.filter('.cur-itm').fadeOut(2000);
			iiWrapper.filter(nex).addClass('cur-itm').fadeIn(2000);						    	
    };
    
		this.loopRun = function()
		{						
			iiCurent++;
			iiCurent = iiCurent > iiCount ? 1 : iiCurent;
			
			this.loopAnimate('#ii'+iiCurent);
		
			// Update navigation buttons
			$('ul li',navWrapper).removeClass('cur-page');
			$('ul li a#p'+iiCurent,navWrapper).parent().addClass('cur-page');																			
		};    
		
		this.loopGoTo = function(obj)
		{						
			if (obj.parentNode.className.indexOf('cur-page') == -1) 
			{
				this.loopAnimate(obj.hash);								
				$('ul li',navWrapper).removeClass('cur-page');
				$(obj).parent().addClass('cur-page');
			}
		}; 		
	};

	$.fn.loopitems = function(options)
	{
		return this.each(function()
		{						 
			var element = $(this);
			if (element.data('loopitems')) return; // Return early if this element already has a loopitems instance  									
			
			element.addClass('loop');
			var loopitems = new LoopItems(element,options);
			element.data('loopitems', loopitems);  // Store plugin object in this element's data  
    });
	};
})(jQuery);

