/* IE6 flicker fix
-------------------------------------------------- */
try { document.execCommand("BackgroundImageCache", false, true); } catch(err){}

jQuery.fn.hasClass = function(c) {
	return this.is('.'+c)
};

/**
 * Escapes special selector symbols
 * @param {String} sel			Selector string, e.g 'film[owner_type_id]'
 * @param {Boolean} [prependIDSelector=true]	Whether to prepend result with '#' or not
 * @type {String}				Escaped selector, e.g '#film\\[owner_type_id\\]'
 */
jQuery.escapeSelector = function(sel, prependIDSelector)
{
	if (typeof prependIDSelector == 'undefined') prependIDSelector = true;
	return (prependIDSelector ? '#' : '') + sel.replace(/(!|"|#|\$|%|&|'|\(|\)|\*|\+|,|\.|\/|:|;|\?|@|\[|\\|\]|\^|`|\{|\||\}|~)/g,'\\$1');
}

// cookie handlers

function setCookie(name, value, exdays)
{
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var value = escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());

	document.cookie = name + "=" + value;
}

function getCookie(name)
{
	var i,x,y;
	var cookies = document.cookie.split(";");

	for (i = 0; i < cookies.length; i++)
	{
		x = cookies[i].substr(0,cookies[i].indexOf("="));
		y = cookies[i].substr(cookies[i].indexOf("=") + 1);
		x = x.replace(/^\s+|\s+$/g,"");
		if (x == name)
		{
			return unescape(y);
		}
	}

	return undefined;
}

/* Tabs switcher */

function showTab(ref, scroll){
	scroll = typeof(scroll) != 'undefined' ? scroll : true;

	var pa = $('A.active',$($(ref).parents('UL')[0]));
	$(pa).removeClass('active');
	$(ref).addClass('active');
	if(scroll) {
		$($(pa).attr('href')).slideUp(300,function(){
			$($(ref).attr('href')).slideDown(300);
		});
	}
	else {
		$($(pa).attr('href')).hide();
		$($(ref).attr('href')).show();
	}
	return false;
}

function showPrevTab(set){
	var tmp1 = $('A',$(set));
	var tmp2;
	$(tmp1).each(function(i){
		if($(this).hasClass('active')){ tmp2 = i; }
	});
	$(tmp1[tmp2]).removeClass('active');
	if(tmp2==0){
		$(tmp1[tmp1.length-1]).addClass('active');
		$($(tmp1[tmp2]).attr('href')).slideUp(300,function(){
			$($(tmp1[tmp1.length-1]).attr('href')).slideDown(300);
		});
	} else {
		$(tmp1[tmp2-1]).addClass('active');
		$($(tmp1[tmp2]).attr('href')).slideUp(300,function(){
			$($(tmp1[tmp2-1]).attr('href')).slideDown(300);
		});
	}
	return false;
}

function showNextTab(set){
	var tmp1 = $('A',$(set));
	var tmp2;
	$(tmp1).each(function(i){
		if($(this).hasClass('active')){ tmp2 = i; }
	});
	$(tmp1[tmp2]).removeClass('active');
	if(tmp2==tmp1.length-1){
		$(tmp1[0]).addClass('active');
		$($(tmp1[tmp2]).attr('href')).slideUp(300,function(){
			$($(tmp1[0]).attr('href')).slideDown(300);
		});
	} else {
		$(tmp1[tmp2+1]).addClass('active');
		$($(tmp1[tmp2]).attr('href')).slideUp(300,function(){
			$($(tmp1[tmp2+1]).attr('href')).slideDown(300);
		});
	}
	return false;
}

/* Login options */
var logintype = 'hover'
var timerUserMenu = '';

function toggleUserMenu(){
	if(logintype!="hover"){
		$("#user-attributes").toggleClass('open');
		$("#user-menu").toggleClass('drop-hidden');
	}
	if($("#user-menu").hasClass('drop-hidden')==false){
		logintype = "click";
	}
	else{
		logintype = "";
	}
}

function hoverUserMenu(){
	window.clearTimeout(timerUserMenu);
	if(logintype!="click"){
		logintype = "hover";
		$("#user-attributes").addClass('open');
		$("#user-menu").removeClass('drop-hidden')
		.hover(cancelHideUserMenu,hideUserMenu);
	}
}
function cancelHideUserMenu(){
	window.clearTimeout(timerUserMenu);
}
function hideUserMenu(){
	if(logintype=="hover"){
		window.clearTimeout(timerUserMenu);
		timerUserMenu = window.setTimeout('realHideUserMenu()',150);
	}
}
function realHideUserMenu(){
	$("#user-menu").addClass('drop-hidden');
	$("#user-attributes").removeClass('open');
}


/* Tooltip actions. */
function showTooltip(ref,txt,opt,move){
	$('body').append('<div id="tooltip"><div id="tooltip-ending"></div><div id="tooltip-inner">' + txt + '</div></div>');
	$('#tooltip').bgiframe();
	if(opt && opt != ''){ helper = opt; } else { helper = ref; }
	if(move && move != ''){
		$('#tooltip').css({
			left: $(helper).offset().left + move + 'px',
			top: $(helper).offset().top + $(helper).height() + 'px'
		});
	} else {
		fit = $(document).width() - $(helper).offset().left - $('#tooltip').width();
		$('#tooltip')[0].style.top = $(helper).offset().top + $(helper).height() + 'px';
		if( fit < 0 ){
			$('#tooltip')[0].style.left = $(helper).offset().left + fit + 'px';
			$('#tooltip-ending')[0].style.backgroundPosition = fit - fit - ($(document).width() - $(ref).offset().left) + 'px 0';
		} else {
			$('#tooltip')[0].style.left = $(helper).offset().left + 'px';
		}
	}
	$(ref).bind('mouseout',function() {
		$('#tooltip').unbind('mouseout');
		$('#tooltip').remove();
		$('IFRAME.bgiframe').remove();
	});
}

(function($){
	/**
	 * @name bgiframe
	 * @type jQuery
	 * @cat Plugins/bgiframe
	 * @author Brandon Aaron (brandon.aaron@gmail.com || http://brandonaaron.net)
	 */
	$.fn.bgIframe = $.fn.bgiframe = function(s) {
		// This is only for IE6
		if ( $.browser.msie && /6.0/.test(navigator.userAgent) ) {
			s = $.extend({
				top     : 'auto', // auto == .currentStyle.borderTopWidth
				left    : 'auto', // auto == .currentStyle.borderLeftWidth
				width   : 'auto', // auto == offsetWidth
				height  : 'auto', // auto == offsetHeight
				opacity : true,
				src     : 'javascript:false;'
			}, s || {});
			var prop = function(n){return n&&n.constructor==Number?n+'px':n;},
				html = '<iframe class="bgiframe"frameborder="0"tabindex="-1"src="'+s.src+'"'+
						   'style="display:block;position:absolute;z-index:-1;'+
							   (s.opacity !== false?'filter:Alpha(Opacity=\'0\');':'')+
							   'top:'+(s.top=='auto'?'expression(((parseInt(this.parentNode.currentStyle.borderTopWidth)||0)*-1)+\'px\')':prop(s.top))+';'+
							   'left:'+(s.left=='auto'?'expression(((parseInt(this.parentNode.currentStyle.borderLeftWidth)||0)*-1)+\'px\')':prop(s.left))+';'+
							   'width:'+(s.width=='auto'?'expression(this.parentNode.offsetWidth+\'px\')':prop(s.width))+';'+
							   'height:'+(s.height=='auto'?'expression(this.parentNode.offsetHeight+\'px\')':prop(s.height))+';'+
						'"/>';
			return this.each(function() {
				if ( $('> iframe.bgiframe', this).length == 0 )
					this.insertBefore( document.createElement(html), this.firstChild );
			});
		}
		return this;
	};
})(jQuery);

/* Exapndable div */
$(document).ready(function() {
	$('#search').submit(function() {
		var searchStringUrl = encodeURIComponent($('#search-string').val());
		$('#search').attr('action', js_site_url + 'static/ElisaSearch/do/' + js_page_id + '/' + searchStringUrl + '&lang=' + js_active_lang);
	});

	$('INPUT.onfocusclear').bind('focus',function () {
		if(!this.oldvalue){
			this.oldvalue = this.value;
		}
		if(this.value == this.oldvalue){
			this.value = '';
			$(this).removeClass('defaultvalue');
		}
	}).bind('blur',function () {
		if(this.value == ''){
			this.value = this.oldvalue;
			$(this).addClass('defaultvalue');
		}
	});
	$('.ihasahover').hover(function(){ $(this).addClass('hover'); },function(){ $(this).removeClass('hover'); });

	handleDataTablesCurrencies.init();

	$('DIV.expandable DIV.expander').parent()
		.click(function()
		{
			if($(this).hasClass('closed'))
			{
				$(this).removeClass('closed');
				$(this).parents('DIV.expandable').eq(0)
					.find('DIV.more').removeClass('hidden').end()
					.find('DIV.main').removeClass('cb04-b').addClass('cb04-c');
			}
			else 
			{
				$(this).addClass('closed');
				$(this).parents('DIV.expandable').eq(0)
					.find('DIV.more').addClass('hidden').end()
					.find('DIV.main').addClass('cb04-b').removeClass('cb04-c');
			}
			return false;
		})
		.hover(
			function()
			{
				$(this).addClass('hover');
			},
			function()
			{
				$(this).removeClass('hover');
			}
		);
	
	$('DIV.expandable DIV.expander').parent().each(function()
	{
		$(this).css({ cursor: 'pointer' });
		if($(this).is(':not(.open)'))
		{
			$(this).addClass('closed');
			$(this).parents('DIV.expandable').eq(0)
				.find('DIV.more').addClass('hidden').end()
				.find('DIV.main').addClass('cb04-b').removeClass('cb04-c');
		}
	});
	
	$('H3.expandable,H3.expandable2').each(function(i,a){
		$(this).addClass('block').removeClass('hidden');
		$('A', a).click(function(){
			if($(this).hasClass('open')){
				$(this).removeClass('open');
				$($('A', a).attr('href')).addClass('hidden');
			} else {
				$(this).addClass('open');
				$($('A', a).attr('href')).removeClass('hidden');
			}
			return false;
		});
		if(!$('A', a).hasClass('open')){
			$($('A', a).attr('href')).addClass('hidden');
		}
	});

	try
	{
		if(top.location.hash && top.location.hash != '#' && $('a[href="'+top.location.hash+'"]').length > 0) {
			showTab($('a[href="'+top.location.hash+'"]', false).get(0));
		}
	}
	catch (e)
	{
	}
	
	$('[data-tooltip]').mouseover(function(e)
	{
		showTooltip(this, $(this).attr('data-tooltip'), '', 38);
	});
});


var handleDataTablesCurrencies = {
	datatables_currency: default_datatables_currency,
	init: function(){
		handleDataTablesCurrencies.datatables_currency = default_datatables_currency; // ($.cookie('datatables_currency') ? $.cookie('datatables_currency') : 'dt_table_1');

		$('.datatables_currency_selector A').click(function(){
			if ($(this).attr('rel') == 'dt_table_2') {
				handleDataTablesCurrencies.datatables_currency = 'dt_table_2';
				// $.cookie('datatables_currency', 'dt_table_2', {path: '/', expires: 1});
			} else {
				handleDataTablesCurrencies.datatables_currency = 'dt_table_1';
				// $.cookie('datatables_currency', 'dt_table_1', {path: '/', expires: 1});
			}
			handleDataTablesCurrencies.switchCurrency();
		});
		handleDataTablesCurrencies.switchCurrency();
	},
	switchCurrency: function(){
		if (handleDataTablesCurrencies.datatables_currency == 'dt_table_1') {
			$('.datatables_currency_selector A[rel="dt_table_2"]').removeClass('active');
			$('.datatables_currency_selector A[rel="dt_table_1"]').addClass('active');
			$('.dt_table_2').hide();
			$('.dt_table_1').show();
		} else {
			$('.datatables_currency_selector A[rel="dt_table_1"]').removeClass('active');
			$('.datatables_currency_selector A[rel="dt_table_2"]').addClass('active');
			$('.dt_table_2').show();
			$('.dt_table_1').hide();
		}
	}
};

function price_format( number, decimals, dec_point, thousands_sep ) {
	var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
	var d = dec_point == undefined ? "." : dec_point;
	var t = thousands_sep == undefined ? "," : thousands_sep, s = n < 0 ? "-" : "";
	var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
	out=s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");

	out=out.replace('.00','.-');
	if(out=='0') return '0.-';

	return out;
}

$(function()
{
	// _var.support.nofwk shows current setting

	// top !== self means I'm inside iframe
	var set_nofwk = Number(top !== self);
	if (
		(typeof(_var) != 'undefined')
		&& ('support_nofwk' in _var)
	)
	{
		var nofwk = Number(_var['support_nofwk']);

		if (set_nofwk != nofwk)
		{
			redirect(window.location.href + '&nofwk=' + set_nofwk);
		}
	}

});


