
jQuery.event.add(window, "load", resizeFrame);
jQuery.event.add(window, "resize", resizeFrame);


//Add Class to first li items of top and footer menus to remove left borders
jQuery(document).ready(function($){
	$('ul#menu-pagrindinis-meniu li,ul#menu-pagrindinis-meniu-en li,ul#menu-pagrindinis-meniu-ru li').first().addClass('first');
	$('ul#menu-pagrindinis-meniu li,ul#menu-pagrindinis-meniu-en li,ul#menu-pagrindinis-meniu-ru li').last().addClass('last');
	$('#fl_menu').floatmenu();
	var $browser = detectBrowserVersion();
	$('body').addClass($browser);
	var wrapper_css = jQuery("meta[name=template_wrapper]").attr('content'); 
	$('#wrapper').addClass(wrapper_css);
	var l18n_close = jQuery("meta[name=l18n_close]").attr('content'); 
	$('.bigimage').colorbox({rel:'popit', close:l18n_close});
});	

function resizeFrame(){
	$fix = $(document).height()-$('#header').outerHeight()-$('#navigation').outerHeight()-$('#footer').outerHeight();
	$rc = $('#rightcolumn').height();
	var vst = $(document).scrollTop();
	
	console.log('resize:'+$(document).height()+' vst:'+vst);
	if($rc<$fix && vst==0){
		console.log('naudoju fixa');
		$('#faux,#pakuote_bg,#vienkartiniai_bg,#iranga_bg,#transportas_bg').height($fix);
	}
}

function detectBrowserVersion(){
	
	var browser = '';
	
	var userAgent = navigator.userAgent.toLowerCase();
	$.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase());
	var version = 0;

	// Is this a version of IE?
	if($.browser.msie){
		browser = 'ie';
		userAgent = $.browser.version;
		userAgent = userAgent.substring(0,userAgent.indexOf('.'));
		version = userAgent;
	}

	// Is this a version of Chrome?
	if($.browser.chrome){
		browser = 'chrome';
		userAgent = userAgent.substring(userAgent.indexOf('chrome/') +7);
		userAgent = userAgent.substring(0,userAgent.indexOf('.'));
		version = userAgent;
		// If it is chrome then jQuery thinks it's safari so we have to tell it it isn't
		$.browser.safari = false;
	}

	// Is this a version of Safari?
	if($.browser.safari){
		browser = 'safari';
		userAgent = userAgent.substring(userAgent.indexOf('safari/') +7);
		userAgent = userAgent.substring(0,userAgent.indexOf('.'));
		version = userAgent;
	}

	// Is this a version of Mozilla?
	if($.browser.mozilla){
		//Is it Firefox?
		browser = 'mozilla';
		if(navigator.userAgent.toLowerCase().indexOf('firefox') != -1){
		userAgent = userAgent.substring(userAgent.indexOf('firefox/') +8);
		userAgent = userAgent.substring(0,userAgent.indexOf('.'));
		version = userAgent;
		}
		// If not then it must be another Mozilla
		else{
		}
	}

	// Is this a version of Opera?
	if($.browser.opera){
		browser = 'opera';
		userAgent = userAgent.substring(userAgent.indexOf('version/') +8);
		userAgent = userAgent.substring(0,userAgent.indexOf('.'));
		version = userAgent;
	}
	return browser;
} 

function pop(url, name, w, h) {
	var new_win;
	if (h.toString().slice(-1) == '%') {
		h = h.slice(0,-1);
		if (!h) h = 75;
		if (screen.availHeight)
			h = parseInt(h)/100 * 2*screen.availHeight-screen.height;
		else
			h = parseInt(h)/100 * screen.height;
	}

	if (w.toString().slice(-1) == '%') {
		w = w.slice(0,-1);
		if (!w) w = 75;
		if (screen.availWidth)
			w = parseInt(w)/100 * 2*screen.availWidth-screen.width;
		else
			w = parseInt(w)/100 * screen.width;
	}
	var features = 'width='+w+',height='+h+',toolbar=0,status=0,menubar=0,directories=0,location=0,scrollbars=yes,copyhistory=0,resizable=yes';

	new_win = open(url, name, features);
	new_win.focus();
	return(false);
}

(function($) {
  $.fn.floatmenu = function(options) {
    // merges the given options with some default options
    var options = $.extend({
      topPadding: 10
    }, options);
    
    return this.each(function() {
      // fetches and initializes the current element.
      var obj = $(this);
      
      var position = $(obj).offset(),
      cssPosition = $(obj).css('position');
      
      // tests if a "position" was set on the element, if not, sets a default
      if(cssPosition == '') {
        cssPosition = 'static';
      }
      
      // attaches an event to listen for scroll events
      $(window).scroll(function(e) {
        // if the window's inner frame passes the top of the element,
        // start moving the menu
        if($(window).scrollTop() > (position.top - Number(options.topPadding))) {
          $(obj).
            css('position','fixed').
            css('top', options.topPadding);
        }
        // the window's inner frame has not passed the menu, reset
        // the objects position
        else {
          $(obj).css('position', cssPosition);
        }
      });
    });
  };
})(jQuery);
