$(document).ready(function() {
	
	// Clear default values
	$('input[type="text"], textarea').focus(function(){ if( this.value == this.defaultValue ) { this.value = ""; }}).blur(function() { if( !this.value.length ) { this.value = this.defaultValue; } });
	
	$('.jqtransform').jqTransform();
	
	formatMenu();
	
	firstNewsItemColored()

	var contId = $('.sliding').attr('id');
	var _initFilter = $('#initFilter').val();
	if (contId == 'catalog' && _initFilter != '') 
		initFilter(_initFilter);

	// Layout setup: top, foot size @ resolution;
	var width = parseInt($(window).width());
	var height = parseInt($(window).height());
	var _height = parseInt($(document).height());
	var wHeight = $('#wrapper').height();
	if (width >= 1400) { var wrapper = 1220; }
	else if (width >= 1250) { var wrapper = 1060; }
	else { var wrapper = 780; }
	
	var marginTop = Math.round((height - 605)/2); 
	var navTop = Math.round((height + 18)/2);
	
	$('.autosize').css('width', wrapper + 'px');
	$('#wrapper').css('margin-top', marginTop + 'px');
	$('#flash').css('margin-top', marginTop + 'px');
	$('#move-left, #move-right').css('top', navTop + 'px');
	
	// Dropdown menu
	$('.hasSub').hoverIntent({
		over: function () { $(this).children('ul').slideDown(200); },
		timeout: 400,
		out: function () { $(this).children('ul').slideUp(200); }
	});
	
	// sliding content: width and position;
	var pos = $('.autosize').offset();
	$('.sliding').css('width', getSlidingWidth() + 'px').css('margin-left', pos.left + 'px');
	$('#contacts').css('width', wrapper + 'px').css('margin-left', pos.left + 'px'); // contacts page == wrapper 
	$('#text-overlay').css('width', pos.left - 10).css('height', marginTop + wHeight + 50 + 'px');
	$('#news-overlay').css('width', pos.left - 10).css('height', height + 'px');
	$('.text-flash').css('right', pos.left - 105);
	$('.news-footer').css('left', pos.left).css('top', (marginTop + 550)); // footer in news, positioned absolute, need left
	
	// content: move left;
	$('#move-left').click(function() {
		var contId = $('.sliding').attr('id');
		if ($('.sliding').is(':animated')) return false; // false if animation is running
		var content_width = parseInt($('.sliding').width());
		var current_pos = parseInt($('.sliding').position().left);
		if (current_pos < 0) {		
			var act_block = $('.active-block');
			var next_block = $('.active-block').prev('div');
			var active_width = parseInt($(next_block).css('width')) + 12; // + 12px margin;

			$(act_block).removeClass('active-block'); // remove active class;
			$(next_block).addClass('active-block'); // add active class to next block;
		
			if (contId == 'catalog')
				$('.sliding').animate({ left: '+='+(active_width*4)+'px'}, 1200, 'easeInOutQuart'); // scroll;
			else if (contId == 'files')
				$('.sliding').animate({ left: '+='+(244*4)+'px'}, 1200, 'easeInOutQuart'); // scroll;
			else
				$('.sliding').animate({ left: '+='+active_width+'px'}, 1200, 'easeInOutQuart'); // scroll;
		}

		return false;

	});
	
	// content: move right;
	$('#move-right').click(function() {
		var contId = $('.sliding').attr('id');
		if ($('.sliding').is(':animated')) return false; // false if animation is running
		var wrapper_width = parseInt($('.autosize').css('width'));
		var content_width = parseInt($('.sliding').width());
		var current_pos = parseInt($('.sliding').position().left);
		var wrapper_width = parseInt($('.autosize').css('width'));
		var max_pos = '-'+(content_width-wrapper_width);
		if (current_pos >= max_pos) {
			var act_block = $('.active-block');
			var next_block = $('.active-block').next('div');
			var active_width = parseInt($(act_block).css('width')) + 12; // + 12px margin;

			$(act_block).removeClass('active-block'); // remove active class;
			$(next_block).addClass('active-block'); // add active class to next block;
		
			if (contId == 'catalog')
				$('.sliding').animate({ left: '-='+(active_width*4)+'px'}, 1200, 'easeInOutQuart'); // scroll;
			else if (contId == 'files')
				$('.sliding').animate({ left: '-='+(244*4)+'px'}, 1200, 'easeInOutQuart'); // scroll;
			else
				$('.sliding').animate({ left: '-='+active_width+'px'}, 1200, 'easeInOutQuart'); // scroll;

		}

		return false;
	
	});
	
	// news content: move right;
	$('#news-move-left').click(function() {
		if ($('.sliding').is(':animated')) return false; // false if animation is running
		var wrapper_width = parseInt($('.autosize').css('width'));
		var content_width = parseInt($('.sliding').width());
		var current_pos = parseInt($('.sliding').position().left);
		var wrapper_width = parseInt($('.autosize').css('width'));
		if (current_pos < 0) {
			var act_block = $('.active-block');
			var next_block = $('.active-block').prev('div');
			var active_width = parseInt($(act_block).css('width')) + 12; // + 12px margin;

			var a_bw = $(act_block).children('.news-img').find('a').attr('href');
			var a_cl = $(act_block).children('.news-img').find('img').attr('src');
			var n_bw = $(next_block).children('.news-img').find('img').attr('src');
			var n_cl = $(next_block).children('.news-img').find('a').attr('href');

			$('.sliding').animate({ left: '+=266px'}, 800, 'easeOutQuart');

			$(act_block).children('.news-img').find('img').attr('src', a_bw);
			$(act_block).children('.news-img').find('a').attr('href', a_cl);
			$(act_block).children('.news-img').animate({width: 254, height: 168}, 650, 'easeInOutQuart', function() {
				$(act_block).find('.news-short').animate({ height: 125 }, 500, 'easeInOutQuart', function() {
					$(act_block).children('.news-inner').children('.news-content').children('.news-full').slideUp(500, 'easeInOutQuart');
				});
			});

			$(next_block).children('.news-img').find('img').attr('src', n_cl);
			$(next_block).children('.news-img').find('a').attr('href', n_bw);
			$(next_block).children('.news-img').animate({width: 519, height: 340}, 650, 'easeInOutQuart', function() {
				$(next_block).find('.news-short').animate({ height: $(next_block).find('.news-short-inner').height() + 15 }, 1000, 'easeInOutQuart', function() {
					$(next_block).children('.news-inner').children('.news-content').children('.news-full').slideDown(1000, 'easeInOutQuart');
				});
			});

			$(act_block).removeClass('active-block'); // remove active class;
			$(act_block).removeClass('active-news-block');
			$(next_block).addClass('active-block'); // add active class to next block;
			$(next_block).addClass('active-news-block');

		}

		return false;

	});		
		
	// news content: move right;
	$('#news-move-right').click(function() {
		if ($('.sliding').is(':animated')) return false; // false if animation is running
		var wrapper_width = parseInt($('.autosize').css('width'));
		var content_width = parseInt($('.sliding').width());
		var current_pos = parseInt($('.sliding').position().left);
		var wrapper_width = parseInt($('.autosize').css('width'));
		var max_pos = '-'+(content_width-616); // 519+12+1
		if (current_pos >= max_pos) {
			var act_block = $('.active-block');
			var next_block = $('.active-block').next('div');
			var active_width = parseInt($(act_block).css('width')) + 12; // + 12px margin;
			
			var a_bw = $(act_block).children('.news-img').find('a').attr('href');
			var a_cl = $(act_block).children('.news-img').find('img').attr('src');
			var n_bw = $(next_block).children('.news-img').find('img').attr('src');
			var n_cl = $(next_block).children('.news-img').find('a').attr('href');
			
			$('.sliding').animate({ left: '-=266px'}, 800, 'easeOutQuart');

			$(act_block).find('.news-short').animate({ height: 125 }, 1, 'easeInOutQuart', function() {
				$(act_block).children('.news-inner').children('.news-content').children('.news-full').slideUp(1000, 'easeInOutQuart');
			});

			$(act_block).children('.news-img').find('img').attr('src', a_bw);
			$(act_block).children('.news-img').find('a').attr('href', a_cl);
			$(act_block).children('.news-img').animate({width: 254, height: 168}, 650, 'easeInOutQuart');
			
			$(next_block).children('.news-img').find('img').attr('src', n_cl);
			$(next_block).children('.news-img').find('a').attr('href', n_bw);
			$(next_block).children('.news-img').animate({width: 519, height: 340}, 650, 'easeInOutQuart', function() {
				$(next_block).find('.news-short').animate({ height: $(next_block).find('.news-short-inner').height() + 15 }, 1000, 'easeInOutQuart', function() {
					$(next_block).find('.news-full').slideDown(1000, 'easeInOutQuart');
				});
			});

			$(act_block).removeClass('active-block'); // remove active class;
			$(act_block).removeClass('active-news-block');
			$(next_block).addClass('active-block'); // add active class to next block;
			$(next_block).addClass('active-news-block');
		}

		return false;

	});
	
	// mooove single news item
	$('.news-block').click(function() {
		if ($(this).hasClass('active-news-block')) // not active one
			return false;
		
		var index = parseInt($(this).attr('id').replace('news-',''));
		var margin = pos.left;
		var slidePos = ((index-1)*266);
		
		var act_block = $('.active-block');
		var next_block = $(this);
		
		var a_bw = $(act_block).children('.news-img').find('a').attr('href');
		var a_cl = $(act_block).children('.news-img').find('img').attr('src');
		var n_bw = $(next_block).children('.news-img').find('img').attr('src');
		var n_cl = $(next_block).children('.news-img').find('a').attr('href');
		
		$('.sliding').animate({ left: '-' + slidePos + 'px'}, 800, 'easeOutQuart'); // scroll;
	
		// transform news items
		$(act_block).children('.news-img').find('img').attr('src', a_bw);
		$(act_block).children('.news-img').find('a').attr('href', a_cl);
		$(act_block).children('.news-img').animate({width: 254, height: 168}, 650, 'easeInOutQuart');

		$(act_block).find('.news-short').animate({ height: 125 }, 1, 'easeInOutQuart', function() {
			$(act_block).children('.news-inner').children('.news-content').children('.news-full').slideUp(1000, 'easeInOutQuart');
		});
		
		$(next_block).children('.news-img').find('img').attr('src', n_cl);
		$(next_block).children('.news-img').find('a').attr('href', n_bw);
		$(next_block).children('.news-img').animate({width: 519, height: 340}, 650, 'easeInOutQuart', function() {
			$(next_block).find('.news-short').animate({ height: $(next_block).find('.news-short-inner').height() + 15 }, 1000, 'easeInOutQuart', function() {
				$(next_block).children('.news-inner').children('.news-content').children('.news-full').slideDown(1000, 'easeInOutQuart');
			});
		});
		
		$(act_block).removeClass('active-block'); // remove active class;
		$(act_block).removeClass('active-news-block');
		$(next_block).addClass('active-block'); // add active class to next block;
		$(next_block).addClass('active-news-block');
	
	});
	
	// reset hovers (safari, ie)
	$('.product-title').css('bottom', '-85px');
	
	// category hover
	$('.product').hoverIntent({
		timeout: 200,
		over: function () { 
			$(this).children('a').children('img.product-cl').animate({ opacity: 1}, 1, function() {
				$(this).parent().parent().children('.product-title').delay(150).animate({ bottom: '0'}, 500, 'easeInOutQuart');
			});
		},
		out: function() { 
			$(this).children('.product-title').animate({ bottom: '-85px'}, 500, 'easeInOutQuart'); 
			$(this).children('a').children('img.product-cl').animate({ opacity: 0}, 1);
		}
	});	
	
	$('.contacts-bt').click(function() {
		if ($('.contacts-form').hasClass('form-active')) { // slide in
			$('.contacts-form').removeClass('form-active');
			$('.contacts-form').animate({right: '-2px'}, 300, 'easeInOutQuart');
		}
		else { // slide out
			$('.contacts-form').addClass('form-active');
			$('.contacts-form').animate({right: '254px'}, 300, 'easeInOutQuart');
		}
		
	})
	
	// Contact form
	$('#cForm').submit(function() {
		var data = $('#cForm').serialize();
		$.ajax({
			type: "POST",
			async: false,
			data: data,
			url: "/ajaxs/contact/" + data,
			success: function(response){
				alert(response.msg);
			}
		});			
		return false;;
	});	
	
});

// Gets sliding content width;
function getSlidingWidth() {
	var id = $('.sliding').attr('id');
	var width = 0;
	if (id == 'files') {
		width = (Math.round($('.file:visible').length/2)*254);
	}
	
	else {
		$('.sliding .block, .sliding .photo-block').each(function() {
			width += parseInt($(this).css('width').replace('px', '')) + 12; 
		});
	}

	return width;

}

function getSlidingWidthVisible() {
	var width = 0;
	$('.block:visible').each(function() {
		width += parseInt($(this).css('width').replace('px', '')) + 12; 
	});
	return width;
}

// Menu format
function formatMenu() {
	$('#menu li').each(function() {
		var liWidth = parseInt($(this).width());
		var ulWidth = 160;
		var ulPos = Math.round((liWidth-ulWidth)/2);
		$(this).children('ul').css('left', ulPos + 'px');
	})
}

function firstNewsItemColored() {
	var bw = $('#news .active-news-block').children('.news-img').children('a').children('img').attr('src'); 
	var cl = $('#news .active-news-block').children('.news-img').children('a').attr('href');
	$('#news .active-news-block').children('.news-img').children('a').attr('href', bw);
	$('#news .active-news-block').find('img').attr('src', cl);
	$('#news .active-news-block').find('.news-short').animate({ height: $('#news .active-news-block').find('.news-short-inner').height() + 15 }, 1000, 'easeInOutQuart', function() {
		$('#news .active-news-block').children('.news-inner').children('.news-content').children('.news-full').slideDown(1000, 'easeInOutQuart');
	});
}

// Back
function goback() { history.go(-1); }

function updateSliding() {
	var currentWidth = parseInt($('.sliding').width());
	$('.sliding').css('width', (getSlidingWidth() - currentWidth) + 'px');
}

function initFilter(filter) {
	_fadeOut(filter);
	_fadeIn(filter)
	
	$('.filter').removeClass('active');
	$('.'+filter).addClass('active');

	count = $('.' + filter).length;
	$('.sliding').css('width', (266 * count) + 'px');
}

function setFilter(filter_id, filter) {
	var count = 0;
	
	// Disable clicking if animation is running or clicked on active filter
	if ($('.block:animated').size() > 0) return false;
	if ($('#filter-'+filter_id).hasClass('active')) return false;
	
	// Switch classes
	$('.filter').removeClass('active');
	$('.block').removeClass('active-block');
	
	// Filter all
	if (filter == 'all') {
		count = $('.block').length;
		_fadeIn(filter);
		$('.block:first').addClass('active-block');
	}
	
	// Specific filter
	else {
		count = $('.' + filter).length;
		_fadeOut(filter);
		_fadeIn(filter)
		$('.'+filter+':first').addClass('active-block');
	}
	
	// Set active filter 
	$('#filter-'+filter_id).addClass('active');
	
	if (count < 4)
		$('#move-left, #move-right').fadeOut('fast');
	else
		$('#move-left, #move-right').fadeIn('fast');

	if ($('.block:animated').length == 0) {
		$('.sliding').css('width', (266 * count) + 266 + 'px');
	}
	
	return false;
}

function setFileFilter(filter_id, filter) {
	var count = 0;
	var fileObj = '';
	
	// Disable clicking if animation is running or clicked on active filter
	if ($('.file:animated').length > 0) return false;
	if ($('#filter-'+filter_id).hasClass('active')) return false;

	$('.sliding').animate({ left: 0 }, 500, 'easeOutQuart', function() {
		
		// Switch classes
		$('.filter').removeClass('active');
		$('.file').removeClass('active-block');

		if ($('.file:visible').length > 0)
			fileObj = '.file:visible';
		else 
			fileObj = '.file';
		
		// Filter all
		if (filter == 'all') {
			$(fileObj).fadeOut(1300, function() { 
				count = Math.round($('.file').length/2);
				$('.sliding').css('width', (count*254) + 'px');
				$('.file').fadeIn(1300, function() {
					if ($(fileObj).length < 8) $('#move-left, #move-right').fadeOut('fast');
					else $('#move-left, #move-right').fadeIn('fast');
				});
			});
			$('.file:first').addClass('active-block');
		}
		
		// Specific filter
		else {
			$(fileObj).fadeOut(1300, function() {
				count = Math.round($('.' + filter).length/2);
				$('.sliding').css('width', (count*254) + 'px');
				$('.' + filter).fadeIn(1300, function() {
					if ($(fileObj).length < 8) $('#move-left, #move-right').fadeOut('fast');
					else $('#move-left, #move-right').fadeIn('fast');
				});
			});
			$('.'+filter+':first').addClass('active-block');
		}
		
		// Set active filter 
		$('#filter-'+filter_id).addClass('active');
		
	});	
	
	return false;
}

function _fadeIn(filter) {
	$('.sliding').animate({ left: 0 });
	
	if (filter == 'all') {
		$('.block').stop().animate({ width: 254, marginRight: 12 }, 650, 'easeOutQuart', function() { 
			$(this).animate({ opacity: 1 }, 2500); 
		});
	}
	
	else {
		$('.' + filter).stop().animate({ width: 254, marginRight: 12 }, 650, 'easeOutQuart', function() { 
			$(this).animate({ opacity: 1 }, 2500); 
		});	
	}
}

function _fadeOut(filter){
	$('.block:not(".' + filter + '")').stop().animate({ opacity: 0 }, 1300, function(){ 
		$(this).animate({ width: 0, marginRight: 0 }, 650, 'easeOutQuart'); 
	});
}
