/* use to load functions after loading page - jquery util*/
$(document).ready(function() {
	//$('a[rel*=facebox], form[rel*=facebox]').facebox();
	
	$('.jqzoom').jqueryzoom( {
		xzoom: 200, //zooming div default width(default width value is 200)
		yzoom: 250, //zooming div default width(default height value is 200)
		offset: 10, //zooming div default offset(default offset value is 10)
		position: "right" //zooming div position(default position value is "right")	
	}
	);
	$('a.tooltip').tooltip({
			showURL: false,
			top: -5,
			left: 10
		}
	);
	$('input.submit_search').tooltip({
			showURL: false,
			top: -5,
			left: 10
		}
	);
	
	$("div.topButton img").not("[src*='hover']").hover(
		function() {
			var image = $(this);
			if(image.attr('src'))
			image.attr('src', 'images/index/button_'+image.attr('name')+'_hover.gif');
		},
		function() {
			var image = $(this);
			image.attr('src', 'images/index/button_'+image.attr('name')+'.gif');
		}
	);
	
	$('.gallery_demo_unstyled').addClass('gallery_demo'); // adds new class name to maintain degradability
	$('.nav').css('display','none'); // hides the nav initially
	
	$('ul.gallery_demo').galleria({
		history   : false, // deactivates the history object for bookmarking, back-button etc.
		clickNext : false, // helper for making the image clickable. Let's not have that in this example.
		insert    : undefined, // the containing selector for our main image. 
							   // If not found or undefined (like here), galleria will create a container 
							   // before the ul with the class .galleria_container (see CSS)
		onImage   : function(image,caption,thumb) { // let's add some image effects for demonstration purposes
				
				// fade in the image & caption
				if(! ($.browser.mozilla && navigator.appVersion.indexOf("Win")!=-1) ) { // FF/Win fades large images terribly slow
					image.css('display','none').fadeIn(1000);
					caption.css('display','none').fadeIn(1000);
				}
				
				// fetch the thumbnail container
				var _li = thumb.parents('li');
				
				// fade out inactive thumbnail
				_li.siblings().children('img.selected').fadeTo(500,0.3);
				
				// fade in active thumbnail
				thumb.fadeTo('fast',1).addClass('selected');
				
				// add a title for the clickable image
				image.attr('title','Nast. obrazek >>');
			},
			onThumb : function(thumb) { // thumbnail effects goes here
				
				// fetch the thumbnail container
				var _li = thumb.parents('li');
				
				// if thumbnail is active, fade all the way.
				var _fadeTo = _li.is('.active') ? '1' : '0.3';
				
				// fade in the thumbnail when finnished loading
				thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);
				
				// hover effects
				thumb.hover(
					function() { thumb.fadeTo('fast',1); },
					function() { _li.not('.active').children('img').fadeTo('fast',0.3); } // don't fade out if the parent is active
				)
			}
		
	});
	//runs slide shows
	MySlideShow2();
	$('#MySlideshow').slideshow(
			{
				width:488,      // width in px
				height:625,     // height in px
				index:0,        // start from frame number N
				time:3000,      // time out beetwen slides
				title:false,     // show title
				titleshow:false,// always show title
				callback:null,  // callback function - call when slide changed - receive index and label
				panel:true,     // show controls panel
				play:true,     // play slideshow
				loop:true,      // looping
				effect:'fade',  // aviable fade, scrollUp/Down/Left/Right, zoom, zoomFade, growX, growY
				effecttime:4000,// aviable fast,slow,normal and any valid fx speed value
				filter:false,    // remove <br/>, empty <div>, <p> and other stuff
				nextclick:false,      // bind content click next slide
				playclick:false,      // bind content click play/stop
				playhover:false,      // bind content hover play/stop
				playhoverr:false,     // bind content hover stop/play (reverse of playhover)
				playframe:false,       // show frame "Play Now!"
				loadframe:true,       // show frame with "loading"
				fullscreen:false,     // in full window size
				imgresize:false,      // resize image to slideshow window
				imgzoom:true,         // zoom image to slideshow window (for smaller side)
				imgcenter:true,       // set image to center
				imgajax:true,         // load images from links
				imglink:true,         // go to external link by click
				linkajax:false,       // load html from links
				
				controls :{         // show/hide controls elements
					'hide':false,    // show controls bar on mouse hover
					'first':true,   // goto first frame
					'prev':true,    // goto previouse frame (if it first go to last)
					'play':true,    // play slideshow
					'next':true,    // goto next frame (if it last go to first)
					'last':true,    // goto last frame
					'help':false,    // show help message
					'counter':true  // show slide counter
				}
			});
	
	$('#newsletter').popup({
        closeKey: 27,
        preloader: true,
        width: 503
    }); 
}
);

function un_block(obj) {
	var iter = 0;
	var input = $(obj).parents().parents().parents().children('div.labCont:eq(2)').children('div.lab2').children('input');
	$(obj).children('select option:selected').each(function(i) {
		if($(this).val() == 'Y') {
			input.attr('price', input.val());
			iter = iter + 1;
		}
		else {
			input.attr('disabled','disabled');
		}
		if(iter != 0) {
			input.removeAttr('disabled');
		}
		else {
			input.val(input.attr('price'));
		}
	});
}

function showDedicationOptions() {
	if($('select#select_dedication').val() == 'Tak') {
		$('div#select_options').css('display', 'block');
	}
	else {
		$('div#select_options').hide('display', 'none');
	}
}


function swapImage(newUrl, altUrl) {
	$('img#imageProduct').attr('src', newUrl);
	$('img#imageProduct').attr('jqimg', altUrl);
}

function checkVarieties(urlToLoad) {
	$('select#rozmiar').attr("disabled","disabled");
	$('select#kolor').attr("disabled","disabled");
	$('div#produktWarianty').load(urlToLoad,
		{
			rozmiar: $('#rozmiar').val(),
			kolor: $('#kolor').val()
		},
		function(responseText) {
			var rozm = $('select#rozmiar').val();
			var kol = $('select#kolor').val();
			$('input#size').val(rozm);
			$('input#colour').val(kol);

			$('select#rozmiar').removeAttr('disabled');
			$('select#kolor').removeAttr('disabled');
		}
	);
	
}

function checkAddCart() {
	if($('#dostepnoscStrong').html() == 'wybierz wariant') {
		alert('Prosze wybrac wariant rozmiaru oraz koloru');
		return false;
	}
	else if($('#dostepnoscStrong').html() == 'Brak' || $('#dostepnoscStrong').html() == 'niedostępny' || $('#dostepnoscStrong').html() == 'niedostepny') {
		alert('Aktualnie produkt w tym wariancie jest niedostepny');
		return false;
	}
	$('form#add_to_cart').submit();
}

function makeAnOrder(newAction) {
	$('form#cartForm').attr({action: newAction});
	$('#cartForm').submit();
}

function showOther() {
	var disp = $('#sendData').css('display');
	if(disp == 'none') {
		$('#sendData').css('display', 'block');
		$('#otherButton').val('Podany wczesniej adres do wysylki');
		$('#otherData').val('Y');
	}
	else {
		$('#sendData').css('display', 'none');
		$('#otherButton').val('Inny adres do wysylki');
		$('#otherData').val('N');
	}
}

function loadcart(object, urlToLoad) {
	var divToLoad = $(object).parent('.date').parent('.userOrder').children('div:last');
	if(divToLoad.css('display') == 'block') {
		divToLoad.css('display', 'none');
	}
	else {
		divToLoad.css('display', 'block');
		divToLoad.html('Ładowanie danych...');
		divToLoad.load(urlToLoad);
	}
}

function windowOpen(url, width, height, scroll) {
	if(!scroll) {
		scroll = 'yes';
	}
	window.open(url, 'name', 'width='+width+',height='+height+',menubar=no,toolbar=no,scrollbars='+scroll);
}

function redirector(link, obj) {
	var url = link+'order_by/'+$(obj).val()+'/';
	window.location = url;	
}

function MySlideShow2(){
	$('div.mySlideShow').everyTime(7000, 'mySlideShow', function(obj){
		var indexToShow = 0;
		var nextIndex = 0;
		var aObjects = $(this).find('div.slide');
		var aActiveObj = $(this).find('div.active');
		
		if($(aActiveObj).length == 0) {
			$(aObjects[0]).addClass('active');
			indexToShow = 0;
		}
		
		//returns index of active class
		if(indexToShow != null) {
			aObjects.each(function(index){
				if($(aObjects[index]).hasClass('active')) {
					indexToShow = index;
				}
				$(aObjects[index]).css('display', 'none');
			});
		}
		
		//checks index if last then next active is first one
		if(indexToShow == aObjects.length-1) {
			nextIndex = 0;
		}
		else {
			nextIndex = indexToShow + 1;
		}
		//shows and hides 
		$(aObjects[indexToShow]).removeClass('active').css('display', 'block');
		$(aObjects[nextIndex]).addClass('active');
		
		
	}, 0);
}

function MySlideShow() {
	//first 2 ale visible
	$('div.mySlideShow div.slide').each(function(index){
		if(index == 0) {
			$(this).css('display', 'block').css('float', 'left');
		}
		else if(index == 1) {
			$(this).css('display', 'block').css('float', 'right');
		}
	});
	$('div.mySlideShow').everyTime(7000, 'mySlideShow', function(obj){
		var indexToShow = 0;
		var nextIndex = 0;
		var aObjects = $(this).find('div.slide');
		var aActiveObj = $(this).find('div.active');
		
		if($(aActiveObj).length == 0) {
			$(aObjects[0]).addClass('active');
			indexToShow = 0;
		}
		//returns index of active class
		if(indexToShow != null) {
			aObjects.each(function(index){
				if($(aObjects[index]).hasClass('active')) {
					indexToShow = index;
				}
				$(aObjects[index]).css('display', 'none');
			});
		}
		//checks index if last then next active is first one
		if(indexToShow == aObjects.length-1) {
			nextIndex = 0;
		}
		else {
			nextIndex = indexToShow + 1;
		}
		//shows and hides 
		$(aObjects[indexToShow]).removeClass('active').css('display', 'block').css('float', 'left');
		$(aObjects[nextIndex]).addClass('active').css('display', 'block').css('float', 'right');
	}, 0);
}

function checkTelForm() {
	var input = $('input[name^="home_phone"]').val();
	if(input.length < 7 && $('select[name^=card_type]').val() == 'pobranie') {
		alert('Proszę wpisać poprawny nr telefonu');
		return false;
	}
}