// Being nice to other libraries
jQuery.noConflict();

// In here $ is jQuery regardless of what other libraries are used
(function($) {
	
	/* blockUI global overrides */
	$.blockUI.defaults.css.padding			= '5px';
	$.blockUI.defaults.css.color			= '#29261b';
	$.blockUI.defaults.css.border			= '2px solid #2e2c23';
	$.blockUI.defaults.css.backgroundColor	= '#f0f0f0';
	$.blockUI.defaults.css.cursor			= 'default';
	$.blockUI.defaults.overlayCSS.cursor	= 'default';
	$.blockUI.message						= '<h1>Please wait...</h1>';
	
	/* Show Tips */
	show_tip = function() {
		if($.browser.msie) {
			$('#tip').text(this.title)
		}
		else {
			$('#tip').queue('fx', []);
			$('#tip').stop();

			$('#tip').text(this.title)
			.show()
			.animate({opacity: 1}, 100);
		}
	}
	
	/* Hide tips */
	hide_tip = function() {
		if(! $.browser.msie) {
			$('#tip')
			.animate({opacity: 1}, 500)
			.fadeOut('slow', function() {
				$(this).html('&nbsp;')
				.show();
			})
		}
	}
	
	
      jQuery.fn.fadeToggle = function(speed, easing, callback) {
        return this.animate({opacity: 'toggle'}, speed, easing, callback);  
      };
	

	
		
	/* Observe Confirmations */
	observe_confirm = function() {
		var link = this.href;
				
		$.ajax({
			url: link, 
			cache: false,
			complete: function(html) { 
				$.blockUI({ message: '<p>'+html.responseText+'</p>'}); 
			}
		});

		$('#confirm_cancel').click(function() {
			$.unblockUI();
			return false;
		});
		
		return false;
	}
	
	/* Display forms below the clicked link */
	show_inline = function() {				
		var link = this.href;
		var el = $(this);
				
		$.ajax({
			url: link,
			cache: false,
			complete: function(html) {
				$('.ajax_add').remove();
				
				$('<div class="ajax_add hidden clearfix"></div>')
				.insertAfter(el.parents().get(1))
				.html(html.responseText);
				
				$('.ajax_add > :not(form)').remove();
				$('.ajax_add').slideDown('fast');
			}
		});
		
		return false;
	}
	
	/* Hide Inline Form */
	hide_inline = function() {
		$('.ajax_add').slideUp('fast');
		return false;
	}

	/* Create Flashdata Container */
	create_container = function() {
		$('<div id="flashdata"><span class="message_content"></span></div>').prependTo('#content');
		return $('#flashdata');
	}

	/* 
	 * Fade out flashdata div (or create if it doesn't exist)
	 * Update message
	 * Fade back in
	 */
	show_flash = function(msg) {
		// Clear timer - just in case
		if(window.timer)
			clearTimeout(window.timer);
				
		var container = $('#flashdata');

		if (! container.get(0)) {
			container = create_container();
			container.hide();
		}

		container.fadeOut('fast', function() {
			$('.message_content').html(msg);
		});
		
		container.fadeIn('fast', hide_flash);
	}

	/* Hide flashdata after 5 seconds (or immediately if it's empty) */
	hide_flash = function() {
		// Clear existing timer to avoid flickering
		if(window.timer)
			clearTimeout(window.timer);

		window.timer = window.setTimeout(function() {
			$('#flashdata').fadeOut("slow", function() {
				$('#flashdata').remove();
			});
		}, 5000);
	}
	
	/* Hijack empty categories */
	observe_empty = function() {
		$('.empty').click(function() {
			var cat = this.id;
			cat = cat.split('_')[1];

			var add_url = BASE_URL + 'member/listings/add/' + cat;

			show_flash('This category is empty.  Be the first to <a href="' + add_url + '">add an entry</a>.');
			return false;
		});
	}
	
	
	// MENU HOVER STANJE
	set_current_menu = function() {

			if (typeof PAGE_NAME=="undefined"){
				//nije definiran page
			} else {
			
				switch(PAGE_NAME) {
				
					case 'home':
					$('a#home').css("background","url(" + BASE_URL + "static/images/menu/m_home_o.jpg) no-repeat");
					$('a#home').css("position", "relative");
					$('a#home').css("top","-7px");
					$('a#home').css("height","41px");
					break;
						
					case 'search':
					$('a#search').css("background","url(" + BASE_URL + "static/images/menu/m_search_o.jpg) no-repeat");
					$('a#search').css("position", "relative");
					$('a#search').css("top","-7px");
					$('a#search').css("height","41px");
					break;
					
					case 'news':
					$('a#news').css("background","url(" + BASE_URL + "static/images/menu/m_news_o.jpg) no-repeat");
					$('a#news').css("position", "relative");
					$('a#news').css("top","-7px");
					$('a#news').css("height","41px");
					break;
					
					case 'events':
					$('a#events').css("background","url(" + BASE_URL + "static/images/menu/m_events_o.jpg) no-repeat");
					$('a#events').css("position", "relative");
					$('a#events').css("top","-7px");
					$('a#events').css("height","41px");
					break;
					
					case 'clients':
					$('a#clients').css("background","url(" + BASE_URL + "static/images/menu/m_clients_o.jpg) no-repeat");
					$('a#clients').css("position", "relative");
					$('a#clients').css("top","-7px");
					$('a#clients').css("height","41px");
					break;

				
				}
			}	
	}
	
	
	
	
	
    addTargetBlank = function(trg){
	    var ex_url = BASE_URL; // your website address
	    var href = $(trg).attr("href");
	    if(href && href.indexOf("http://")!=-1 && href.indexOf(ex_url)==-1){
	        $(trg).attr("target","_blank");
	    }
	}
	
	scrollPage = function(id) {
		$.scrollTo( id, 1200, {queue:true} );
		return false;
	}
	
	

	set_flir_headings = function (){
		if(this.flir_replaced) return;
	    FLIR.replace([ 'h1.glavni-naslov', 'h2.flir', 'h1.naslov-novosti'], new FLIRStyle({mode:'wrap'}));
	    this.flir_replaced=true;
	}
	
	mga_nekretnine = function () {
		window.open('http://www.mganekretnine.hr/','mganekretnine');
	}
	
	mga_nekretnine = function () {
		window.open('http://www.mganekretnine.hr/','mganekretnine');
	}
	

	init = function() {

	   	  $("a").each(function(){addTargetBlank(this)});
	   	  
	   	  set_current_menu();
	   	//  set_flir_headings();
	   	 
	   	
	   	// na klik nestani flashdata notifications
	   	$('.flashdata').click(function() {$(this).fadeOut('slow')});
	   	
	   	// na klik dismiss messages
	 	$('.message > .dismiss > a').click(function() {
	        var value = $(this).attr("href");
	        var id = value.substring(value.indexOf('#') + 1);

	        $("#" + id).fadeOut('slow', function () { });

	        return false;
	 	});
	 	

	   	
		
		// Registration Optional Field toggle
		$('#show_optional').toggle(
			function() {
				$('#optional').show();
				$(this).toggleClass('less');
				$(this).text('Hide Optional Fields');
				return false;
			},
			function() {
				$('#optional').hide();
				$(this).toggleClass('less');
				$(this).text('Show Optional Fields');
				return false;
			}
		);
		
		// Show/hide inline forms
		$('a.inline').toggle(
			show_inline,
			hide_inline
		);

		// Hide/show stuff
		$('.js_hide').hide();
		$('.js_show').show();

		// Fade out flashdata
		hide_flash();
		
		// Hijack clicks on empty categories - show flash_message
		observe_empty();
		
		// Tips
		$('.tip').mouseover(show_tip);
		$('.tip').mouseout(hide_tip);
		
		// Add confirmations to delete links
		$('.confirm').click(observe_confirm);
		
		// Force focus
		//$('.field_error, .focus').get(0).focus();
	}

	$(document).ready(init);
	
	/* Safari bug workaround */
	$(window).bind('load', function() {
		$('.js_hide').hide();
		$('.js_show').show();
	});


})(jQuery);