/*
 * jQuery Browser Plugin
 * Version 2.3
 * 2008-09-17 19:27:05
 * URL: http://jquery.thewikies.com/browser
 * Description: jQuery Browser Plugin extends browser detection capabilities and can assign browser selectors to CSS classes.
 * Author: Nate Cavanaugh, Minhchau Dang, & Jonathan Neal
 * Copyright: Copyright (c) 2008 Jonathan Neal under dual MIT/GPL license.
 * JSLint: This javascript file passes JSLint verification.
 */
(function($){$.browserTest=function(a,z){var u='unknown',x='X',m=function(r,h){for(var i=0;i<h.length;i=i+1){r=r.replace(h[i][0],h[i][1]);}return r;},c=function(i,a,b,c){var r={name:m((a.exec(i)||[u,u])[1],b)};r[r.name]=true;r.version=(c.exec(i)||[x,x,x,x])[3];if(r.name.match(/safari/)&&r.version>400){r.version='2.0';}if(r.name==='presto'){r.version=($.browser.version>9.27)?'futhark':'linear_b';}r.versionNumber=parseFloat(r.version,10)||0;r.versionX=(r.version!==x)?(r.version+'').substr(0,1):x;r.className=r.name+r.versionX;return r;};a=(a.match(/Opera|Navigator|Minefield|KHTML|Chrome/)?m(a,[[/(Firefox|MSIE|KHTML,\slike\sGecko|Konqueror)/,''],['Chrome Safari','Chrome'],['KHTML','Konqueror'],['Minefield','Firefox'],['Navigator','Netscape']]):a).toLowerCase();$.browser=$.extend((!z)?$.browser:{},c(a,/(camino|chrome|firefox|netscape|konqueror|lynx|msie|opera|safari)/,[],/(camino|chrome|firefox|netscape|netscape6|opera|version|konqueror|lynx|msie|safari)(\/|\s)([a-z0-9\.\+]*?)(\;|dev|rel|\s|$)/));$.layout=c(a,/(gecko|konqueror|msie|opera|webkit)/,[['konqueror','khtml'],['msie','trident'],['opera','presto']],/(applewebkit|rv|konqueror|msie)(\:|\/|\s)([a-z0-9\.]*?)(\;|\)|\s)/);$.os={name:(/(win|mac|linux|sunos|solaris|iphone)/.exec(navigator.platform.toLowerCase())||[u])[0].replace('sunos','solaris')};if(!z){$('html').addClass([$.os.name,$.browser.name,$.browser.className,$.layout.name,$.layout.className].join(' '));}};$.browserTest(navigator.userAgent);})(jQuery);

/*
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
jQuery.cookie=function(name,value,options){if(typeof value!='undefined'){options=options||{};if(value===null){value='';options=$.extend({},options);options.expires=-1;}var expires='';if(options.expires&&(typeof options.expires=='number'||options.expires.toUTCString)){var date;if(typeof options.expires=='number'){date=new Date();date.setTime(date.getTime()+(options.expires*24*60*60*1000));}else{date=options.expires;}expires='; expires='+date.toUTCString();}var path=options.path?'; path='+(options.path):'';var domain=options.domain?'; domain='+(options.domain):'';var secure=options.secure?'; secure':'';document.cookie=[name,'=',encodeURIComponent(value),expires,path,domain,secure].join('');}else{var cookieValue=null;if(document.cookie&&document.cookie!=''){var cookies=document.cookie.split(';');for(var i=0;i<cookies.length;i++){var cookie=jQuery.trim(cookies[i]);if(cookie.substring(0,name.length+1)==(name+'=')){cookieValue=decodeURIComponent(cookie.substring(name.length+1));break;}}}return cookieValue;}};

function debug() {
	if(console) {
		console.log(arguments);
	}
}

var IE6 = $.browser.msie && $.browser.versionX == 6;
var IE7 = $.browser.msie && $.browser.versionX == 7;
var IE8 = $.browser.msie && $.browser.versionX == 8;
var content;
$('html').addClass('js');

/*
 * Drop shadow plugin
 *
 * @author slava
 */
(function($){
	$.widget("ui.shadow", {
		_init: function() {
			/*if ($.browser.name == 'safari' || ($.browser.firefox && $.browser.versionNumber >= 3.5)) { // maybe useful in future
				this.js = false;
			} else {*/
				this.js = true;
				var o = this.options;
				var el = this.element;
				var elId = el[0].id;
				var shadowID = elId + '-' + Math.round(Math.random() * 1000) + '-shadow';

				if (IE6) {
					var shadow = $('<div class="js-shadow" id="' + shadowID + '-shadow"></div>');
				} else {
					var shadow = $('<div class="js-shadow" id="' + shadowID + '-shadow"><span class="sh-b tl"></span><span class="sh-b tc"></span><span class="sh-b tr"></span><span class="sh-b lc"></span><span class="sh-b rc"></span><span class="sh-b bl"></span><span class="sh-b bc"></span><span class="sh-b br"></span></div>');
				}

				el.before(shadow);
				this.shadow = shadow;

				if (o.hide) {
					this.hide();
				} else {
					this.show();
				}
			//}
		},

		update: function() {
			var el = this.element;
			var o = this.options;

			var position = el.position();

			if (IE6) {
				this.shadow.css({
					height: (el.outerHeight() + 8) + 'px',
					left: (position.left - 8) + 'px',
					top: (position.top - 8) + 'px',
					width: (el.outerWidth() + 8) + 'px'
				});
			} else {
				this.shadow.css({
					height: (el.outerHeight() - 1) + 'px',
					left: position.left + 'px',
					top: (position.top) + 'px',
					width: (el.outerWidth() - 1) + 'px'
				});
			}

			var z = el.css('z-index');
			if(z == 'auto') z = 0;
			if(z < this.shadow.css('z-index')) {
				el.css('z-index', parseInt(this.shadow.css('z-index'), 10) + 1)
			}
		},

		show: function() {
			if (this.js) {
				this.shadow.show();
				this.update();
			}
		},

		hide: function() {
			if (this.js) {
				this.shadow.hide();
			}
		},

		toggle: function() {
			if (this.js) {
				if (this.shadow.is(':visible')) {
					this.hide();
				} else {
					this.show();
				}
			}
		},

		destroy: function() {
			if (this.js) {
				this.element.removeData(this.widgetName)
				this.shadow.remove();
			}
		}
	});

	$.extend($.ui.shadow, {
		defaults: {
			hide: false
		}
	});
})(jQuery);

$(function(){
	content = $('#content');

	// cluetip's default settings redefined
	if (typeof $.cluetip != 'undefined') {
		$.fn.cluetip.defaults = $.extend($.fn.cluetip.defaults, {
			activation: 'click',
			dropShadow: false,
			hideLocal: false,
			leftOffset: 30,
			local: true,
			showTitle: false,
			topOffset: 20,
			width: 300
		});
		$(document).bind('click keyup', function(event){
			if (event.type == 'click' || (event.type == 'keyup' && event.keyCode == 27)) {
				$(this).trigger('hideCluetip');
			}
		});
	}

	if($.browser.firefox) {
		var sethome = $('#top-bar a.set-home');
		sethome.cluetip({
			leftOffset: -sethome.width(),
			positionBy: 'fixed'
		});
	}

	$('#footer').hover(
		function() {
			$(this).css('z-index', 3);
		},
		function() {
			$(this).css('z-index', '');
		}
	);

	if (IE6) {
		$('.mainmenu-item .dropdown-menu .item').hover(
			function () { $(this).addClass('hover'); },
			function () { $(this).removeClass('hover');	}
		);
	}

	$('.mainmenu-item-title, .dropdown-menu').shadow({hide: true});

	$('.mainmenu-item:has(.dropdown-menu) .mainmenu-item-title .title').mouseenter(
		function () {
			hideHoverItem();
			var item = $(this).closest('.mainmenu-item');
			item.addClass('mainmenu-item-hover');
			item.find('.mainmenu-item-title, .dropdown-menu').shadow('show');
		// move menu left when it crosses window width
			var dropMenu  = item.find('.dropdown-menu'),
				leftSideX = item.offset().left + dropMenu.width(),
				diff 	  = Math.round(document.body.offsetWidth - leftSideX),
				moveable  = item.find('.dropdown-menu, .js-shadow:last')
			if (diff + 20 < 0)
				moveable.css('margin-left', (diff - 20) + 'px')
			else
				moveable.css('margin-left', '0px')
	});

	//$('.mainmenu-item').mouseleave(hideHoverItem);

	$('#content, #content-clear').mouseenter(hideHoverItem)

	var $menuItems = $('.mainmenu-item'),
		$document  = $(document);

	function hideHoverItem()
	{
		$hoverItem = $menuItems.filter('.mainmenu-item-hover');

		if ($hoverItem.length)
		{
			$hoverItem.removeClass('mainmenu-item-hover');
			$hoverItem.find('.mainmenu-item-title, .dropdown-menu').shadow('hide');

			$document.trigger('hideCluetip');
		}

	}

	$('.mainmenu .dropdown-menu a[class!=bubble-link]').click(function(){
		if (this.href.indexOf('#') > -1) {
			document.location.replace(this.href);
			if (this.href.substr(0, this.href.indexOf('#')) == document.location.toString().substr(0, document.location.toString().indexOf('#'))) {
				document.location.reload(true);
			}
		}
	});

	// firefox hover bug fix
	if ($.browser.firefox) {
		$('#SearchField').hover(function(){
			$('.mainmenu-top .mainmenu-item-hover').removeClass('mainmenu-item-hover').find('.shadow').shadow('hide');
		})
	}

	$('#login-form').shadow({
		hide: true
	});

	/*
	 * Show/hide openid auth form
	 */
	$('#header-auth .login-link').click(function(){
		var auth = $('#header-auth').toggleClass('active');
		$('#login-form').shadow('toggle');
		$('#header-auth:visible #LoginOpenIDURL').focus();

		$(document).bind('mousedown.auth keydown.auth', function(event){
			var form = $(event.target).closest('#header-auth');

			if (!(form.length) || (event.keyCode && event.keyCode == $.ui.keyCode.ESCAPE)) {
				auth.removeClass('active');
				$('#login-form').shadow('hide');

				$(this).unbind('mousedown.auth keydown.auth');
			}
		});
	});

	/*
	 * float bubble
	 */

	var floatBubble = $('#top-menu-bubble');

	if (floatBubble.length)
	{
		var fbContent = floatBubble.find('.fb-c');
		var firstFBTarget = $('.mainmenu-top .mainmenu-item:first');
		var defaultFBOffset = $('.mainmenu-top .mainmenu-item:first').offset().left - 24;
		var prevFBTarget = firstFBTarget[0].id.replace('top-menu-', '');
		var fbHover = false;
		var animationSpeed = 200;

		floatBubble.hover(
			function() {
				fbHover = true;
			},
			function() {
				fbHover = false;
				if (floatBubble.position().left != defaultFBOffset) {
					setTimeout(returnFB, 100);
				}
			}
		);
	}

	function titleEnter(event) {
		var $this = event.data;

		var id = $this[0].id.replace('top-menu-', '');
		var text = floatBubble.find('.' + id);

		if(text.length && id != prevFBTarget) {
			fbHover = true;

			floatBubble.stop();
			fbContent.stop();
			floatBubble.find('.fb-ar').stop();

			fbContent.width(fbContent.width());
			var width = getBubWidth(text);
			var left = $(this).offset().left - 14;

			var windowWidth = $(window).scrollLeft() + $(window).width() - 24;

			if(left + width > windowWidth) {
				var arLeft = 20 + ((left + width) - windowWidth) + 28 + 10;
				left = left - ((left + width) - windowWidth) - 28 - 10;
				floatBubble.find('.fb-ar').animate({
					left: arLeft
				}, animationSpeed, 'linear');
			} else {
				floatBubble.find('.fb-ar').animate({
					left: 20
				}, animationSpeed, 'linear');
			}

			//floatBubble.find('.fb-in').hide();
			setTimeout(function() {
				floatBubble.find('.fb-in').hide();
				text.show();
			}, animationSpeed/2);

			floatBubble.animate({
				left: left
			}, animationSpeed, 'linear');
			prevFBTarget = id;
			fbContent.animate({
				width: width
			}, animationSpeed, 'linear', function(){
				//text.show();
			});
		}
	}

	function titleLeave(event) {
		fbHover = false;
		if (floatBubble && floatBubble.position().left != defaultFBOffset) {
			setTimeout(returnFB, 100);
		}
	}

	$('.mainmenu-top .mainmenu-item-title a').each(function(){
		$(this).bind('mouseenter.fb', $(this.parentNode.parentNode), titleEnter);
	});

	$('.mainmenu-top .mainmenu-item').each(function(){
		var $this = $(this);
		$this.bind('mouseleave.fb', $this, titleLeave);
	});

	function returnFB() {
		if (!fbHover) {
			$('.mainmenu-top .mainmenu-item-title:first a').trigger('mouseenter.fb');
		}
	}

	function getBubWidth(text) {
		text.css({
			display: 'block',
			position: 'absolute',
			visibility: 'hidden'
		});
		var newWidth = text.outerWidth() + 1;
		text.css({
			display: 'none',
			position: 'static',
			visibility: 'visible'
		});

		return newWidth;
	}

	function winFBResize() {
		if( $('.mainmenu-top').length > 0 )
		{
			defaultFBOffset = $('.mainmenu-top .mainmenu-item:first').offset().left;
			floatBubble.css({
				left: defaultFBOffset - 14
			});
		}
	}

	$(window).resize(function(){
		winFBResize();
	});

	winFBResize();
	floatBubble.css({visibility: 'visible'});
	/*
	 * end: float bubble
	 */

	/*
	 * context ads show after load
	 */
	$('.bnr-context > div').show();

	$('#rate-chart').show(); //IE7 display bug
	/*
	 * end: context ads show
	 */

	/*
	 * share buttons
	 */
	$('#clix-share').bind('click', function () {
	    _gaq.push(['_trackEvent', 'Share', 'Clix']);
	})
	$('#addtoany-share').bind('click', function () {
	    _gaq.push(['_trackEvent', 'Share', 'AddToAny']);
	})
	/*
	 * end: share buttons
	 */

	/*
	 * Shorten the URL with byst.ro
	 */
	var url   = $('#result-link-url'),
        link  = $('#result-link-code'),
        shorten = $('#shorten-url');

	shorten.click(function()
	{
	    var $self = $(this);

        if (url.val() != '' && !(/byst\.ro/).test(url.val()))
        {
	        $.get(
	            '/net/api/tinyurl',
	            {
	                long: url.val(),
	                option: 1
	            },
	            function(xml) {
	                var tiny = $(xml).children('result').children('tiny').text(),
	                	linkText = $(link.val()).text();

	                if (tiny == '') tiny = url.val();
	                url.val(tiny);
	                link.val('<a href="' + tiny + '">' + linkText + '</a>');

	                $self.css('visibility', 'hidden');
	                url.prev = url.val();
	            },
	            'xml'
	        );
        }
        return false;
	});

	url.prev = url.val();
	setInterval(checkInputChange, 1000);
	function checkInputChange ()
	{
	    if (url.prev != url.val()) {
	        shorten.css('visibility', 'visible');
	        url.prev = url.val();
	    }
	}

	/*
	 * end: Shorten the URL
	 */

	/*
	 * alcohol/density smart fixed position
	 */
	var $drinkImage = $('#drink-image');
    if ($drinkImage.length)
    {
        $(window).scroll(function (e) {
            var vScroll;
            if (window.pageYOffset)
                vScroll = window.pageYOffset;
            else
                vScroll = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;

            if (vScroll > 400)
                $drinkImage.addClass('fixed');
            else
                $drinkImage.removeClass('fixed');
        });
     }
});

function setCookie(name, value, expiredays, path, domain, secure) {
   if (expiredays) {
      var exdate = new Date();
      exdate.setDate(exdate.getDate() + expiredays);
      var expires = exdate.toGMTString();
   }
   document.cookie = name + '=' + value +
   ((expiredays) ? '; expires=' + expires : '') +
   ((path) ? '; path=' + path : '') +
   ((domain) ? '; domain=' + domain : '') +
   ((secure) ? '; secure' : '');
}

function getCookie (name) {
	var cookie = ' ' + document.cookie;
	var search = ' ' + name + '=';
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(';', offset);
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}


customSlider = function (settings) {
	var t = this;
	t.slideByStep = false;
	$.extend(t, settings);

	t.picker = t.container.children('#' + t.picker_id);

	t.picker.delta = Math.round(t.picker.width() / 2);

	t.minX = 0;
	t.maxX = t.maxVal; //t.container.width() - 9;

	if (t.defaultValue) {
		t.picker[0].style.left = t.defaultValue + t.picker.delta - 2 + 'px';
	}

	t.container.bind('mousedown', this, t.initSlide);

	return this;
}

customSlider.prototype = {
	initSlide: function (e) {
		e.preventDefault();
		e.stopPropagation();

		var that = e.data;

		var offset = Math.round($(this).offset().left) + 6;

		that.slide(e, offset);

		var active = true;

		$(document).bind('mousemove', that, function (e) {
			e.preventDefault();
			if (!active) { return; }

			var timer = setTimeout (function () {
				active = true;
				clearTimeout(timer);
			}, 1);

			active = false;
			that.slide(e, offset);

		})
		.mouseup(function () {
			$(this).unbind('mousemove mouseup');
		});
	},

	slide: function (e, offset) {
		var that = e.data;
		var picker = that.picker;

		var position = e.clientX - offset - picker.delta + 2;
		position = Math.min(Math.max(that.minX, position), that.maxX);

		var value = position * Math.round((that.maxVal + that.minX) / that.maxX) - that.minX;

		if (that.slideByStep) {
			picker[0].style.left = value + picker.delta - 2 + 'px';
		} else {
			picker[0].style.left = position + 'px';
		}

		if (that.fn && that.fn.onChange) {
			that.fn.onChange.call(that.fn.that, value);
		}
	}
}

var aSiteColors = [ 'ED3A3A', 'ED3A3A', 'ED393A', 'ED3939', 'ED3A3B', 'ED3A41',
'ED3B48', 'ED3C50', 'ED3E58', 'ED3F63', 'ED406D', 'ED4178', 'ED4283', 'ED438F',
'ED459A', 'ED47A6', 'ED49B1', 'ED4BBC', 'ED4DC5', 'ED50CF', 'ED52D8', 'ED55E0',
'ED57E7', 'EC5AED', 'E85EF0', 'E262F4', 'DB66F5', 'D46AF5', 'CC6EF5', 'C373F5',
'BA78F5', 'B17DF5', 'A782F5', '9D88F5', '938DF5', '8993F5', '7E98F5', '759DF5',
'6CA1F5', '63A6F5', '5AAAF5', '53AEF5', '4DB1F5', '47B5F5', '42B7F4', '40B8F5',
'40B8F5', '40B8F5', '40B8F5', '40B8F5', '3EB9F4', '3BBBF5', '37BEF5', '32C0F5',
'2DC3F5', '28C6F5', '22CAF5', '1CCDF5', '17D1F5', '11D4F4', '0CD7F3', '08DAF1',
'05DDED', '02E0EA', '01E1E4', '01E3E0', '01E3DC', '01E3D7', '02E5D2', '02E5CE',
'03E6C8', '04E6C3', '06E6BC', '07E6B6', '07E6B0', '09E6A9', '0BE6A3', '0DE69C',
'0EE695', '12E689', '17E67D', '1CE670', '21E665', '27E659', '2EE64F', '35E646',
'3CE63E', '45E637', '50E630', '5DE62A', '6AE625', '77E61F', '87E61A', '95E615',
'A2E613', 'A9E611', 'B0E60F', 'B8E60E', 'C0E60C', 'C6E60A', 'CDE609', 'D4E607',
'DAE606', 'E0E605', 'E6E504', 'EBE402', 'EFE202', 'F4E001', 'F8DE00', 'FBDB00',
'FED800', 'FED400', 'FED000', 'FECC00', 'FEC700', 'FEC200', 'FEBD00', 'FEB800',
'FEB200', 'FEAD01', 'FEA602', 'FEA003', 'FE9A04', 'FE9206', 'FE8B07', 'FE8308',
'FE7D09', 'FE760B', 'FE700C', 'FE6A0D', 'FE650E', 'FE610F', 'FE5E0F', 'FE5E0F'];



/* search form */
function SaveSearch (val) {
	setCookie('last_search', escape(val), 1, '/', BaseServer);
}

function fixFooterPosition() {
	if($.browser.msie && $.browser.versionX == 6) {
		$('#footer').css('position', 'static');
		$('#footer').css('position', '');
	}
}

function setHome(obj, url) {
	if($.browser.msie) {
		obj.style.behavior = 'url(#default#homepage)';
		obj.setHomePage(url);
		return false;
	} else {
		return true;
	}
}
