$(function() {

	$(window).hashchange( function(){

		var oldElementIndex = $('.tovar-nadpis').index($('.tovar-nadpis.selected_article'));
		if (oldElementIndex == -1) {
		    oldElementIndex = 0;
		}
		var newElementIndex = $('.tovar-nadpis').index($('.tovar-nadpis:has(#l-'+(unescape(self.document.location.hash.substring(1)))+')'));
		if (newElementIndex == -1) {
		    newElementIndex = 0;
		}
		changeArticles(oldElementIndex, newElementIndex);
	});


	$('.tovar-cely').css({
		'display':'none',
		'border':'none',
		'opacity': 0
	});
	$('.tovar-cely a[name]').remove();
	$('.tovar-cely:first').before('<div id="article_title_container"></div>');
	$('.tovar-nadpis').clone().prependTo("#article_title_container");
	$('.tovar-cely .tovar-nadpis').remove();
	setActiveArticleByLocationHash();

	$('#menu, #menu_top, #menu_bottom, #drop, #logo').fadeTo(0,0);

	$(document).ready(function() {
		$('#menu, #menu_top, #menu_bottom').animate( { opacity:1 }, 1000 );
		$('#logo').delay(1200).animate( { opacity:1 }, 2500 );
	});
	$(function() {

		if($.browser.msie) { 
			$('#content').css({"left": "800px" });
			$('#body-in').css({"width":"100%", "overflow": "hidden" });
			$('#content').delay(300).animate({
					"left": "0"
				}, 800, function() {
					$('#body-in').css({"width":"", "overflow": "" });
				}
			);
		} else {
			$('#content').css({"opacity":"0", "position":"relative", "left": "800px" });
			$('#content').css({"left": "800px" });
			$('#body-in').css({"width":"100%", "overflow": "hidden" });
			$('#content').delay(300).animate({
					"left": "0",
					"opacity": "1"
				}, 800, function() {
					$(this).css({"position": "static"});
					$('#body-in').css({"width":"", "overflow": "" });
				}
			);
		}
	});

	// menu
	$('#menu ul#nav').removeClass('nav');

	$('#menu ul li.sub ul').css('width', $(this).parent().width());
	$('#menu ul li.sub ul li').css('width', $(this).parent().parent().width());

	$('#menu ul li.sub ul').fadeTo(0,0).css('display', 'none');
	/*
	$('#menu ul li.sub').hover(
		function () {
			$(this).children('ul').stop().css('display', 'block').fadeTo(200, 0.95);
		},
		function () {
			$(this).children('ul').fadeTo(400,0, function () {
			    $(this).css('display', 'none');
			});
		}
	);
	*/
});

// order form AJAX

$(document).ready(function() {
	$('#order_form .polozka_druh select').bind('change', function() {
		if ($(this).val() == '-') {
			$(this).parent().parent().children('.polozka_vek').children('select').attr('disabled', 'disabled');
			$(this).parent().parent().children('.polozka_termin_odberu').children('select').attr('disabled', 'disabled');
		} else {
			$(this).parent().parent().children('.polozka_vek').children('select').load('objednavka-ajax.php', { ajax: '', id_tovar: $(this).val(), vek: '' }).removeAttr('disabled');
			$(this).parent().parent().children('.polozka_termin_odberu').children('select').load('objednavka-ajax.php', { ajax: '', id_tovar: $(this).val(), termin: '' }).removeAttr('disabled');
		}
	});
});


// gallery

$(function() {

	$('.photo').css('opacity', 0.8);

	$('.photo').bind('focusin mouseenter', function() {
		$(this).animate({
			opacity: 1
		}, 300);
	});
	$('.photo').bind('focusout mouseleave', function() {
		$(this).animate({
			opacity: 0.8
		}, 300);
	});
});

// fancybox

$(document).ready(function() {
	$('a:has(.photo), a:has(.popup_photo)').fancybox({
		'transitionIn'  : 'elastic',
		'transitionOut' : 'elastic',
		'titlePosition' : 'inside',
		'overlayOpacity': 0.7,
		'overlayColor'  : '#111'
	});
});

// fader top baners

function slideSwitch() {
	var $active = $('#slideshow img.active');
	if ( $active.length == 0 ) $active = $('#slideshow img:last');
	var $next =  $active.next().length ? $active.next() : $('#slideshow img:first');
	$active.addClass('last-active');
	$next.css({opacity: 0.0})
		.addClass('active')
		.fadeTo(2000, 1, function() {
			$active.removeClass('active last-active');
		});
}
function slideSwitchPoultry() {
	var $active = $('#slideshow_left img.active');
	if ( $active.length == 0 ) $active = $('#slideshow_left img:last');
	var $next =  $active.next().length ? $active.next() : $('#slideshow_left img:first');
	$active.addClass('last-active');
	$next.css({opacity: 0.0})
		.addClass('active')
		.fadeTo(2000, 1, function() {
			$active.removeClass('active last-active');
		});
}

function setActiveArticleByLocationHash() {

    var oldElementIndex = $('.tovar-nadpis').index($('.tovar-nadpis.selected_article'));
	$('.tovar-nadpis:eq('+ oldElementIndex +')').removeClass('selected_article');
	$('.tovar-cely:eq('+ oldElementIndex +')').css({
		'display': 'none',
		'opacity': 0
	});
	if (unescape(self.document.location.hash.substring(1))) {
		$('#id-'+(unescape(self.document.location.hash.substring(1)))).css({
			'display':'block',
			'opacity': 1
		});
		$('#l-'+(unescape(self.document.location.hash.substring(1)))).parent().addClass('selected_article');
	} else {
		$('.tovar-cely:first').css({
			'display':'block',
			'opacity': 1
		});
		$('.tovar-nadpis:first').addClass('selected_article');
		
	}
	createPrevAndNextLinks();
}

function createPrevAndNextLinks() {
	// navigacne sipky pre listovanie 
	
	var article_link_prev = $('.tovar-nadpis.selected_article').prev().children('a').attr('href');
	var article_link_next = $('.tovar-nadpis.selected_article').next().children('a').attr('href');
	
	$('.article_navigation_arrow').remove();
	
	if (article_link_prev != undefined) {
		$('#page').append('<a href="'+article_link_prev+'" id="article_link_prev" class="article_navigation_arrow" style="position:absolute;top:390px;left:-30px;"><img src="image/gyron/article_prev.png" alt="PREV"></a>');
	}
	if (article_link_next != undefined) {
		$('#page').append('<a href="'+article_link_next+'" id="article_link_next" class="article_navigation_arrow" style="position:absolute;top:390px;right:-30px;"><img src="image/gyron/article_next.png" alt="NEXT"></a>');
	}
}

function changeArticles(oldElementIndex, newElementIndex) {
	$('#main-in').css({
		'position':'relative'
	});
	$('#body-in').css({"width":"100%", "overflow": "hidden" });
	
	$('.tovar-cely:eq('+ oldElementIndex +')').css({
		'display': 'block',
		'position': 'absolute',
		'left': '0px',
		'width': '738px',
		'opacity': 1
	}).stop().animate({
		'opacity': 0,
		'left': '-900px'
	}, 600, function() {
		$('.tovar-nadpis:eq('+ oldElementIndex +')').removeClass('selected_article');
		$('.tovar-cely:eq('+ oldElementIndex +')').css({
			'display': 'none',
			'position': '',
			'left': '',
			'width': '',
			'opacity': 0
		});

		$('.tovar-cely:eq('+ newElementIndex +')').css({
			'display': 'block',
			'position': 'absolute',
			'left': '900px',
			'width': '738px',
			'opacity': 0
		}).animate({
			'opacity': 1,
			'left': '0px'
		}, 900, function() {
			$('.tovar-nadpis:eq('+ newElementIndex +')').addClass('selected_article');
			$('.tovar-cely:eq('+ newElementIndex +')').css({
				'position': '',
				'left': '',
				'width': ''
			});
			$('#main-in').css({
				'position':'',
				'overflow:':''
			});
			$('#body-in').css({"width":"", "overflow": "" });
			createPrevAndNextLinks();
		});
	});
}

setInterval( "slideSwitch()", 6000 );
setInterval( "slideSwitchPoultry()", 6000 );

