(function($){

var handle_tracker = function() {
	$("a[href^='http://'], a[href^='https://']").not('[href^="' + cms_systempath + '"]')
		.addClass("offSite")
		.attr('target', '_blank')
		.bind('click keypress', function(ev) {
			var code=ev.charCode || ev.keyCode;

			if((!code || (code && code == 13)) && pageTracker !== undefined) {
				pageTracker._trackEvent('Outbound', $(this).context.hostname, $(this).attr('href'));
			}

			return false;
		});
}

var handle_facebook = function() {
	if (!$('#fb-root').length)
		return;

	window.fbAsyncInit = function() {
		FB.init({
			appId: '223752184320128',
			status: true,
			cookie: true,
			xfbml: true
		});
	};

	var e = document.createElement('script'); e.async = true;
	e.src = document.location.protocol + '//connect.facebook.net/pl_PL/all.js';
	document.getElementById('fb-root').appendChild(e);
};

var handle_content_scroll = function(){
	var $s = $('#scroller');
	var $sc = $('#scroller_content');
	var $ss = $('#scrollable');
	var $s_up = $s.find('.scrollUp');
	var $s_up_d = $s_up.find('div');
	var $s_down = $s.find('.scrollDown');
	var $s_down_d = $s_down.find('div');
	var ss_height = $ss.outerHeight(true);
	var ss = $ss.get(0);
	var ss_max_top = (ss_height - $s.height()) + 20;
	
	// CONFIG
	var s_speed = 25;
	var s_step = 4;
	
	if(ss_height > $s.height()){
		$s_up.hide();
		$s_down.show();
		
		var up_t = null;
		var down_t = null;
		
		var top = 0;
		var m = 1;
		
		$s_up.hover(function(){
			$s_down.show();
			window.clearInterval(down_t);
			$s_up_d.stop(true,true).fadeIn('slow');
			
			top -= s_step*2;
			if(top < 0) top = 0;
			$sc.scrollTop(top);
			
			up_t = window.setInterval(function(){
				top -= s_step*m;
				if(top < 0) top = 0;
				$sc.scrollTop(top);
				m+=0.025;
				
				if(top == 0){
					window.clearInterval(up_t);
					$s_up_d.stop(true,true).fadeOut(500);
					$s_up.fadeOut(500);
					return false;
					
				}
			}, s_speed);
		},function(){
			$s_up_d.stop(true,true).fadeOut('slow');
			window.clearInterval(up_t);
			m = 1;
		});
		
		$s_down.hover(function(){
			$s_up.show();
			window.clearInterval(up_t);
			$s_down_d.stop(true,true).fadeIn('slow');
			
			top += s_step*2;
			if(top >= ss_max_top) top = ss_max_top;
			$sc.scrollTop(top);
			
			down_t = window.setInterval(function(){
				top += s_step*m;
				if(top >= ss_max_top) top = ss_max_top;
				$sc.scrollTop(top);
				m+=0.025;
				
				if(top >= ss_max_top){
					window.clearInterval(down_t);
					$s_down_d.stop(true,true).fadeOut(500);
					$s_down.fadeOut(500);					
					return false;
				}
			}, s_speed);
		},function(){
			$s_down_d.stop(true,true).fadeOut('slow');
			window.clearInterval(down_t);
			m = 1;
		});
	}
}

var handle_fb_box = function(){
	$('#facebook .opener').mouseover(function(){
		$('#facebook').animate({left: '0px'} , 500);
	});
	$('#facebook').mouseleave(function(){
		$('#facebook').animate({left: '-292px'} ,{duration: 500,queue: false});
	});
}

var handle_contact = function() {
		var form_wrap = $('#contactForm_frame'); 
		var form = $('#contactForm_frame #contactForm');
	
		form.validate({
			errorPlacement: function(error, element) {
				error.insertBefore(element.parents("td,.row").children("div.clr"));
			}		
		}); 
		
		$('a.submit', form).click(function(ev) {
			ev.preventDefault();

			if (!form.valid())
				return false;   
				
			var submit = $(this);

			var services = [];
			$('[name=service]:checked',form).each(function() {services.push($(this).val());});
				
			var data = {
				action: $('[name=action]', form).val(),
				username: $('[name=username]', form).val(),
				useremail: $('[name=useremail]', form).val(),
				content: $('textarea[name=content]', form).val(),
				phone: $('[name=phone]', form).val(),
				date: $('[name=date]', form).val(),
				services: services.join(', '),
				stylista: $('[name=stylista] :selected', form).val(),
				url: location.href
			};
			
			$('p.status', form_wrap).hide();
			$('p.wait', form_wrap).show();
			submit.hide();
			form.hide();

			
			$.post(submit.attr('href'), data, function(resp) {
				if (resp == 'err') {
					$('p.status', form_wrap).hide();
					$('p.error', form_wrap).show();
				}
				else if (resp == 'ok') {
					$('p.status', form_wrap).hide();
					$('p.ok', form_wrap).show();
				}
			});

			return false;
		});	
}

var handle_fancybox = function(){
	jQuery(".photo_frame .image").fancybox({
		'transitionIn'	:	'fade',
		'transitionOut'	:	'fade',
		'speedIn'		:	600, 
		'speedOut'		:	200, 
//		'swf'			:	{wmode: 'transparent'},
		'height'		:	550,
		'width'			:	992,
		'overlayShow'	:	false
	});
	
	$('.cardnr').focus();

	$('.cwn').fancybox({
		'padding': 0
	});	
	
}

var handle_photo_cycle = function(){

	var galleries = $('.gallery-box');
	var galleries_cycle = [];
	galleries.each(function(key, value){
		var gallery = $(this);
		galleries_cycle[key] = $('.photo_frame .content', gallery).cycle({ 
			fx:     'fade', 
			speed:	1000, 
			timeout: 3000, 
			pager:  $('.photo_frame_navigator', gallery),
			pagerAnchorBuilder: function(index){
				return $('.photo_frame_navigator', gallery).find('a').get(index);
			},
			pauseOnPagerHover: 1,
			before: function(){
			
			},
			after: function(){
			
			},
			pause: 1
		});		
	});
	
	var stylists = $('.stylist');
	var imgs = $('.photo_frame .content img');
	if(imgs.length == stylists.length){
		stylists.each(function(key, value){
			$(this).click(function(){
				galleries_cycle[0].cycle(key).cycle('pause');
				stylists.removeClass('current');
				$(this).addClass('current');
				$('.photo_frame_navigator a').hide();
				return false;
			});
		});
	}
	
	var gallery_handle = $('.events, .gallery');
	gallery_handle.each(function(key, value){
		$(this).click(function(){
			galleries.hide();
			$.each(galleries_cycle, function(){
				$(this).cycle('pause');
			});
			galleries.eq(key).show();
			galleries_cycle[key].cycle('resume');
			gallery_handle.removeClass('current');
			$(this).addClass('current');
			return false;
		});
	}).eq(0).click();
	
	$('a', galleries).show();
}

var handle_panorams = function(){
	if(window.location == cms_systempath+"/"){	
	
		$('.panoram').click(function(){
            $('#bigpanorama div').html('');
            $('#bigpanorama div').flash({
                src: cms_systempath+'/UserFiles/Flash/'+$(this).data('name'),
                width: 900,
                height: 500
            });
            $("#panoramaminis img").css("border"," 1px solid #161616");
            $(this).css("border"," 1px solid #CCC");			
		}).eq(0).click();

		
		$('#panorams').show().fancybox({
			'padding' : 0,
			'width': 992,
			'height': 680,
			'overlayColor' : '#101010',
			'onClosed'	: function(){$('#panorams').html('')}
		}).click().unbind('click');		
		
	}
}

var handle_link_height = function(){
	
	var menu = $('#menu');
	if(menu){
		var height = $('#menu').height();
		var li = $('#menu ul li');
		var li_height = parseInt(height/li.length);
		$('#menu ul li').css('height', li_height+'px');
		$('#menu ul li').css('line-height', li_height+'px');
		menu.css('visibility','visible');
	}
	
}

var Handle_show_calendar = function(){
	$('.showcalendar').datetimepicker({
		stepMinute: 15,
		timeText: 'Czas',
		hourText: 'Godziny',
		minuteText: 'Minuty'	
	});
}

$(function() {
	handle_tracker();
	handle_facebook();
	handle_photo_cycle();
	handle_panorams();
	handle_fb_box();
	handle_contact();
	handle_fancybox();
	handle_link_height();
	Handle_show_calendar();
	
	$('a').focus(function(){ $(this).blur(); });
	
	if($('#scroller_content') && $('#scroller_content').length == 1){
		handle_content_scroll();
	}
	$('.justify').fullyJustify({properties: ['letter-spacing']});
	


});

})(jQuery);

