jQuery.exists = function(selector) {return ($(selector).length > 0);}

(function(d, s, id) {
		  var js, fjs = d.getElementsByTagName(s)[0];
		  if (d.getElementById(id)) {return;}
		  js = d.createElement(s); js.id = id;
		  js.src = "//connect.facebook.net/et_EE/all.js#xfbml=1&appId=175838425803073";
		  fjs.parentNode.insertBefore(js, fjs);
		}(document, 'script', 'facebook-jssdk'));

$(function(){
	
	root_path = location.protocol + "//" + location.host;
	ajax_path = root_path + "/_ajax";
	pathname = location.pathname;
	
	//logosSlider.init();
	
	$('#gallery a').lightBox();
 
	$(function(){
		$("select").uniform();
	});

	//open banner in new window
	$('.target_blank').click(function(){
		window.open(this.href);
		return false;
	});
	
	if ($('#slider').length > 0)
	{
		$('#slider').anythingSlider({
			autoplay: 'true',
			delay: '5000',
			pauseOnHover: 'true',
			onSlideComplete : function(slider){
			}
		});
	}
	
	if ($('#special-scroller').length > 0)
	{
		$("#special-scroller").jcarousel({
				animation: 400,
				auto: 2,
				scroll: 1,
				vertical: true,
				wrap: 'circular',
				initCallback: specialScrollerInit,
				setupCallback: specialScrollerSetup
		});
	}
	
	product.init();
	
	//Clear input on click
	$('.clear_me').click(function(){
	
		var orig_value = $(this).val();
		
		if ($(this).val() == $(this).attr('rel')){
			$(this).val("");
		}
		
		$(this).blur(function (){
			if ($(this).val()==""){
				$(this).val(orig_value);
			}
		});
	});	
	
	$('.back,.back a').click(function(){
		window.history.back(1);
		return false;
	});
	
	$('.contact-form input').focus(function(){
		$(this).removeClass('error');
	});
	
	$('.plan').click(function(){
		
		var flashvars = 'xmlURL=/xml/&staticURL=/map_static.xml';
		if ($(this).attr('rel')){
			var position = $(this).attr('rel').split('|');
			flashvars += '&floor='+position[0]+'&id='+position[1];
		}

		$.fancybox({
			autoScale: false,
			type: 'swf',
			href: '/swf/yk_plaan.swf',
			width: 970,
			height: 670,
			margin: 0,
			padding: 0,
			overlayColor: '#000',
			overlayOpacity: 0.75,
			swf: {
				flashvars: flashvars,
				wmode: 'window'
			}
		});
		
		return false;
	
	});
	
	if ($('div#logoscroll').length > 0){
		$('div#logoscroll').flash({
			swf: '/swf/logoscroll.swf',
			allowScriptAccess: true,
			flashvars: { xmlUrl: '/xml/' },
			height: 70,
			width: 970,
			wmode: 'transparent'
		});
	}
	
	$('.search legend').click(function(){
		$(this).hide();
		$('.search input[type=text]').focus();
	});
	$('.search input[type=text]').blur(function(){
		if ($(this).val() == '')
		{
			$(this).prev().show();
		}
	});
	$('.search input[type=text]').focus(function(){
		if ($('.search legend').is(':visible'))
		{
			$('.search legend').hide();
		}
	});
	
	$('.open-layer').click(function(){
		var target = $('#'+$(this).attr('rel'));
		var link = $(this);
		var linkPos = Math.round(link.position().left);
		var linkWidth = Math.round(link.outerWidth());
		
		$(target).children('.arrow').css('left', linkPos + linkWidth - 2);
		
		$(target).toggle(0, function(){
			if ($(this).is(':visible'))
			{
				link.addClass('close-layer');
			}
			else {
				link.removeClass('close-layer');
			}
		});
		return false;
	});
	
});

function specialScrollerInit(carousel)
{
	  carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });
    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
}

function specialScrollerSetup(carousel)
{
	$("#special-scroller").show();
	
	$('#special-scroller .text').each(function(i) {
			var ah = $(this).height();
			var ph = $(this).parent().height();
			var mh = Math.ceil((ph - ah) / 2);
			$(this).css('padding-top', mh);
		});
}

var product = {
		
		init: function(){
			this.makeProductImagesGrayscale();
			this.addEvents();
		},
		
		addEvents: function(){
			
			$('#shopImages canvas,#shopImages img').live('mouseenter', function(){
				var src = $(this).closest('a').attr('href');
				$('#shopImage').attr('src', src);
				Pixastic.revert(this);
			});
			
			$('#shopImages img').live('mouseleave', function(){
				Pixastic.process(this, "desaturate");
			});
			
			$('#shopImages img, #shopImages canvas').live('click', function(){
				return false;
			});
			
		},
		
		makeProductImagesGrayscale: function(){
			
			$('#shopImages img').each(function(){
				Pixastic.process(this, "desaturate");
			})
		}
}

/*
jQuery(document).ready(function() {
	
	jQuery("#mycarousel-3").jcarousel({
			scroll: 1,
			initCallback: mycarousel_initCallback,
			// This tells jCarousel NOT to autobuild prev/next buttons
			buttonNextHTML: null,
			buttonPrevHTML: null
	});
	
});
*/ 

function mailt(str1,str2,el)
{
	el.href = 'mailto:' + str1 + '@' + str2;
}

/*
jQuery(document).ready(function() {
    jQuery('#mycarousel-2').jcarousel({
			scroll: 1,
			wrap: 'circular',
			auto: 1,
			animation: 1400,
			initCallback: mycarousel_initCallback2
    });
});
*/


/**
* We use the initCallback callback
* to assign functionality to the controls
*/
function mycarousel_initCallback(carousel) {
	
	jQuery('.jcarousel-control a').bind('click', function() {
		carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
		return false;
	});
	
	jQuery('.jcarousel-scroll select').bind('change', function() {
		carousel.options.scroll = jQuery.jcarousel.intval(this.options[this.selectedIndex].value);
		return false;
	});
	
	jQuery('#mycarousel-next').bind('click', function() {
		carousel.next();
		return false;
	});
	
	jQuery('#mycarousel-prev').bind('click', function() {
		carousel.prev();
		return false;
	});
};

/*
var logosSlider = {
	
	scrollerTimeoutId: 0,
	width: 0,
	
	init: function(){
		
		this.setScrollerSize();
		this.startScrolling();
		this.addEvents();
	},
	
	addEvents: function(){
		
		$('#logos-container').hover(logosSlider.stopScrolling, logosSlider.startScrolling);
		
	},
	
	setScrollerSize: function(){
		
		logosSlider.width = $('#logos-slider li').length * 160;
		$('#logos-slider').css('width', logosSlider.width+'px');
		
	},
	
	stopScrolling: function(){
		
		logosSlider.makeLogosColor();
		clearTimeout(logosSlider.scrollerTimeoutId);
		
	},
	
	scroll: function(){
		
		clearTimeout(logosSlider.scrollerTimeoutId);
		var container = $('#logos-container');
		var offset = logosSlider.width - 960;
		
		if (container.scrollLeft() + offset < logosSlider.width){
			container.scrollLeft(container.scrollLeft() + 2);
			logosSlider.scrollerTimeoutId = setTimeout("logosSlider.scroll();", 40); 
		} else {
			clearTimeout(logosSlider.scrollerTimeoutId);
			container.scrollLeft(0);
			logosSlider.startScrolling();
		}
	},
	
	startScrolling: function(){
		
		logosSlider.makeLogosGrayscale();
		logosSlider.scroll();
	},
	
	makeLogosGrayscale: function(){
		
		$('#logos-slider img').each(function(){
			Pixastic.process(this, "desaturate");
		})
	},
	
	makeLogosColor: function(){
		
		$('#logos-slider canvas,#logos-slider img').each(function(){
			Pixastic.revert(this);
		})
	}
}
*/
