var $ = jQuery;
$(document).ready(function() {


	function AjaxLoader(RenderIn){
		$(RenderIn).html('<img src="/themes/softworks/images/ajax-loader.gif" alt="Loading" class="AjaxLoader" />');
	}
	
	PrzewinElementyNaStronie('.LosowaOferta')
	PrzewinElementyNaStronie('.LosoweWybrane')

	function PrzewinElementyNaStronie(Lista) {		
		var ParentElement = $(Lista).parent().attr('id');
		var ListaID = Lista.replace('.','');
		var curritem=0;
		var newsitems = $(Lista+" li").hide().size();
		if(newsitems =='1') {
				$('#'+ParentElement+' li').show();
				return false;
		}		
    
		$(Lista+" li:eq("+curritem+")").show();
		setInterval(przewin,5000); // czas prezentacji migawki w milisekundach
		var html = '<ul id="'+ListaID+'">'
		if(newsitems >1) {
			for(i=0 ; i<=newsitems-1 ; i++) {
				var css;
				if(i=='0') {css ='show';}
				else {css = 'hide'}
				html += '<li title="'+ListaID+'" class="'+css+' change" id="Numer'+i+'"><span class="Numer"></span</li>';
			}
			html += '</ul>';
			$('#'+ParentElement+' .NawigacjaOferty').html(html)
		}
		function przewin() {
			var countstop = $('#'+ParentElement+' .stop').size();
			
			if(countstop >0) {return false;}				
				$('#'+ParentElement+' .show').removeClass('show');
				$(Lista+" li:eq("+curritem+")").fadeOut("slow"); 
				$('#'+ParentElement+" .NawigacjaOferty li:eq("+curritem+")").addClass('show') 
				curritem = ++curritem%newsitems;
			$(Lista+" li:eq("+curritem+")").fadeIn("slow");
		}
	
		$('#'+ParentElement).mouseover(function() {
			$('#'+ParentElement+' .show').addClass("stop");				
		}).mouseout(function() {
			$('#'+ParentElement).removeClass('stop');
		});
    
		$('.change').click(function(Lista) {
        var Lista = $(this).attr('title');
				var getNR = $(this).attr('id').replace(/[^0-9]+/g, '');
				$('#'+Lista+' li').removeClass('show');
				$(this).addClass('show')    
				$('.'+Lista+' li').hide();
				$('.'+Lista+' li:eq('+ getNR +')').show();
			
		});	    
    
    		
	}



  // emd nawigacji
	
	$(".SwitchFullPart").hover(function() {
		
		var PartOpis = $(this).text();
		var OpisTitle = $(this).attr("title");
					
		if(OpisTitle.length < 30) {
				return false;
		}
		$(this).addClass("FullPart");
		$(this).attr("title",PartOpis);
		$(this).text(OpisTitle);
	})
		
	function ZliczProdukty(){
		AjaxLoader("#MojKoszykTop");
		$("#MojKoszykTop").load("Koszyk/ZliczProdukty");
	}	
	
	ZliczProdukty();	
	
	$("#PytanieProdukt").click(function() {	
		$("#FormularzPytania").slideDown();
	});
	
	$("#Form_SprawdzPrzedWyslaniem_action_Wyslij").click(function() {
		var uri = window.location;
		$('#Response').load(uri+'/SprawdzPrzedWyslaniem',$('#Form_SprawdzPrzedWyslaniem').serializeArray());
		return false;
	});
	
	$('<div id="AjaxCartOutOfBody"><div id="OutOfBodyBelka"></div><div id="RenderCartOnRight" style="display: none"></div><span id="HideRightCart" style="display:none">&raquo; Ukryj</span></div>').appendTo('body');
	$("#OutOfBodyBelka").mouseenter(function() {
		$(this).parent().animate({'right':'0px'},100);
		AjaxLoader("#RenderCartOnRight");
		$("#RenderCartOnRight").show();
		$("#RenderCartOnRight").load("Koszyk?akcja=ajaxview").ajaxComplete(function() {$("#HideRightCart").show();});
		
	});
	$("#HideRightCart").click(function() {
		$(this).hide();
		$("#RenderCartOnRight").hide();	
		$(this).parent().animate({'right':'-305px'},100);
	})

})

