window.addEvent ("domready", function () {
	product = new Products();
});

//uruchomine tylko w widoku produktu
function init_product (price, id, n) {
	//ustaw cene produktu
	this.price = price;
	cart.setPrice (this.price, 1, "false", 1, 1);
	
	//zainicjalizuj rozwijanie dodatkowych opcji do znakowania
	product.init_mark_price (id);
	product.init_mark_slide (id);	
	
	cart.set_overlay(n);
}


var Products = new Class ({
	price_color: '',
		
	initialize: function () {
		this.cont = $('view_content');
		this.is_print = false;
		this.gall_effect = new Array ();
		this.mark_price = new Array ();
		this.calc_nadruki = new Array();
		this.fx_calc_nadruki = new Array();
		this.is_print =  new Array ();
		this.fx_mark_price = new Array ();
		this.fx_cover = new Fx.Tween($('cover'));
	},

	
	hideAll: function () {
		this.cont.getChildren().each (function (item, i) {
			item.setStyle('display', 'none');
		})
	},		
	
	change_photo: function (src, id) {

		var new_img = new Element('img', {
			'src':src,
			'styles' : {
				'display': 'none'
			}
		})

		if (!$defined(this.gall_effect[id])) {
			this.gall_effect[id] = new Fx.Tween($('product_gallery_'+id), {
				duration: 700,
				link: 'cancel'
			});
		}

		this.gall_effect[id].onComplete = function () {
			$('gall_loader_' + id).fade('show')
			
			var myImages = new Asset.images (src, {
				onComplete: function () {
					$('prod_gallery_'+id).getFirst('img').destroy();
					myImages.inject($('prod_gallery_'+id));
					var src2 = src.split('/');
					if( src2.length == 4 )
						$('prod_gallery_'+id).setProperty('href','thumb/forCart/'+src2[3]);
					else
						$('prod_gallery_'+id).setProperty('href','thumb/forCart/'+src2[4]);
					var milkbox = new Milkbox();
					milkbox.reloadGalleries();
					this.gall_effect[id].start('opacity', 1);
					$('gall_loader_' + id).fade('hide')
				}.bind(this)
				
			})

			
			this.gall_effect[id].onComplete = $empty;
		}.bind(this)
	
		
		this.gall_effect[id].start('opacity', 0);
		
	},
	
	init_mark_price: function (id) {
		if ($defined($('mark_price_cont_'+id))) {
			this.mark_price[id] = $('mark_price_cont_'+id);
			this.fx_mark_price[id] = new Fx.Slide(this.mark_price[id], {
				transition: Fx.Transitions.Quad.easeInOut,
				duration: 700
			}).hide();
		}
	},
	
	init_mark_slide: function (id) {
		if ($defined($('calc_nadruki_' + id))) {

			this.calc_nadruki[id] = $('calc_nadruki_'+id);
			this.fx_calc_nadruki[id] = new Fx.Slide (this.calc_nadruki[id], {
				transition: Fx.Transitions.Quad.easeInOut,
				duration: 700,
				link: 'cancel'				
			}).hide();			
		}
		
		if ($defined($('checkbox_nadruki_' + id))) {
			var chckb = $('checkbox_nadruki_'+id).addEvent('click', function(){
				if (chckb.checked) {
					this.calc_nadruki[id].setStyle('display', 'block');
					this.fx_calc_nadruki[id].slideIn();
					this.is_print[id] =true
				}
				else {
					this.fx_calc_nadruki[id].slideOut();
					this.is_print[id] = false
				}
			}.bind(this))
		}
	},

	
	setPhotosGallery: function (photos) {
		this.photos = photos;
	},
	
	findPage: function (obj) {
		//find Page
		var my_cookie = Cookie.read ('bookmarks');
		var pg =''
		
		my_cookie = JSON.decode (my_cookie)
		my_cookie.root.each (function (item, index) {
			if (item.controller == obj[0] && item.action == obj[1] && item.id == obj[2]) {
				pg = item.page;
			}
		})
		return pg;		
	},
	
	remove: function (controller, action, id) {
		if (elem = $(controller + '_' + action + '_' + id)) {
			elem.dispose();
		}
	},
	
	activate: function (index) {
		$('view_content').getChildren()[index].setStyle('display', 'block');
	
	},
	
	fadeCoverIn: function (height) {
		//$('cover').setStyle('height', $('content').getStyle('height').toInt());
		this.fx_cover.cancel();
		this.fx_cover.set('opacity', 1);		
	},
	
	fadeCoverOut: function (height) {


		//$('cover').setStyle('height', $('content').getStyle('height').toInt());
		this.fx_cover.cancel();
		this.fx_cover.start('opacity', 0);		
	},
	
	changePage: function (controller, action, id, page, prior) {
		if (!$defined(page)) {
			page = 1;
		}
		var prod_cont = $(controller + '_' + action + '_' + id);
		
		new Request.HTML({
					url: controller +'/' + action +'/' + id +'/' + page  + '/' + prior,
					method: 'post',
					link: 'cancel',
					update: ($(controller + '_' + action + '_' + id)) ? $(controller + '_' + action + '_' + id) : '',
					onRequest: function(){

			/* pokaz przykrywke, jest to bialy div o tle #ffffff o takich samych rozmiarach jak konterner glowny (id="contener")
			 * dzieki temu najpierw przykrywa widoku produktu, pojawia sie na nim loader, a pozniej animacyjnie zmieniamy jego
			 * alfa na 0. Jesli zmienialibysmy przezroczystosc calego widoku produktow z kodem html to prawdopodobnie bardzo wolno
			 * by to chodzilo. */
						if ($(controller + '_' + action + '_' + id)) {
							var height = $('content').getStyle('height').toInt()
							

							var cart_h = $('cart').getStyle('height').toInt();
							if (cart_h > height) {
								height = cart_h - 32
							}	
							this.fadeCoverIn(height);							
							$('product_loader').setStyle('visibility', 'visible');//pokaz loader
						}
					}.bind(this),
					
					onSuccess: function(responseTree, responseElements, responseHTML){
			/* samo ukrycie wszystkich elementow podczas nowego elementu (widoku) nie wystarczy. W momencie kiedy uzytkownik
			/  szybko klikal zdarzalo sie ze dwa widoki na raz bylo aktywne. Dlatego dla bezpieczenstwa po wykonaniu zadania jeszcze raz
			/  staramy sie ukryc wszystkie widoki produktow */
						if ($(controller + '_' + action + '_' + id)) {
							this.hideAll();
							
							prod_cont.setStyle('visibility', 'hidden');
							prod_cont.setStyle('display', 'block')
	
							var height = $('content').getStyle('height').toInt() - 36;	
							
							var cart_h = $('cart').getStyle('height').toInt();
							if (cart_h > height) {
								height = cart_h - 32
							}	
							//ustaw rozmiar przykrywki na taka sama wysokosc jak kontener glowny, oraz animacyjnie usun przykrywke
							this.fadeCoverOut(height);
							$('product_loader').setStyle('visibility', 'hidden');//ukryj loader
							prod_cont.setStyle('visibility', 'visible');
						} else {
							var height = $('content').getStyle('height').toInt() - 36;								
							var cart_h = $('cart').getStyle('height').toInt();
							if (cart_h > height) {
								height = cart_h - 32
							}								
							this.fadeCoverOut(height);
							$('product_loader').setStyle('visibility', 'hidden');//pokaz loader							
						}
					}.bind(this)
				}).send()
		
	
	//write page
		var index;
		
		var t = Cookie.read ('bookmarks');
		t = JSON.decode (t);
			
			t.root.each(function(item, i){
				if (item.id == id && item.controller == controller && item.action ==action) {
					item.page =page;
				}
			})

			var t_cookie = JSON.encode(t);
	
			
			Cookie.write('bookmarks', t_cookie, {
				duration: 0,
				path: '/'
			})	
	
	
	
	},
	
	//kalkulacja produktu, znakowania itp
	calculate: function (id) {
		if (!this.calculating && !cart.moving_busy && ! cart.removing_busy) {
			this.calculating = true;	
			if (!this.is_print[id]) {
				this.fx_mark_price[id].slideOut();		
			}		
			request_calculate(id,this.is_print[id])
		}
	},
	
	update_calculation: function (response, is_print) {
		this.hide_calculate_cover (response.id);
		this.calculating = false;
		$('mark_price_'+response.id).set('html', response.format.wartosc);

		//ZAKTUALIZUJ CENE W KALKULATORZE
		var total_price_div = $('mark_price_'+response.id);
		if (this.price_color == '') {
			this.price_color = total_price_div.getStyle('color');
		}

		var my_tween = new Fx.Tween(total_price_div,{
			transition: Fx.Transitions.Quad,
			duration: 100,
			onComplete: function () {
				total_price_div.set('tween', {
					transition: Fx.Transitions.Quad.easeOut,
					duration: 1200					
				}).tween ('color', this.price_color);
			}.bind(this)
		})
		my_tween.start('color', '#00f00');		
		
		//ZAKTUALIZUJ I POKAZ CENE JEDNOSTKOWA Z NADRUKIEM
		// TYLKO JESLI LICZYMY CENE Z NADRUKIEM

		if (is_print) {

				this.fx_mark_price[response.id].slideIn();				
	
		//	if (!$defined (this.price_mark_p)) {
				this.price_mark_p = this.mark_price[response.id].getElements('.price2')[0];
		//	}
			
			this.price_mark_p.set('html', response.format.price_product_print_one);		
			
			var my_tween2 = new Fx.Tween(this.price_mark_p,{
				transition: Fx.Transitions.Quad,
				duration: 100,
				onComplete: function () {
					this.price_mark_p.set('tween', {
						transition: Fx.Transitions.Quad.easeOut,
						duration: 1200					
					}).tween ('color', this.price_color);
				}.bind(this)
			})
			my_tween2.start('color', '#00f00');			
		}	
	
		
	},
	
	show_calculate_cover: function (id) {
		$('przelicz_price_cover_' + id).setStyle('display', 'block');
		if (this.is_print) {
			$('prod_print_cover_' + id).setStyle('display', 'block');
		}
	},
	
	hide_calculate_cover: function (id) {
		$('przelicz_price_cover_' + id).setStyle('display', 'none');
		$('prod_print_cover_' + id).setStyle('display', 'none');
	},	
	
	//sprawa ze obiekt (produkt) moze byc przeciagany do koszyka
	// id_arr - tablica id produktow dzieki ktoremu bedziemy mieli dostep do ich instancji
	setDragable: function (controller, action, id) {
		/*var view_cont = $(controller + '_' + action + '_' + id);

		var dropElement = $('cart_content');
		
		//pobierz wszystkie divy z produktammi
		var products = view_cont.getElements('.product .thumb').getFirst('img');
		
		products.each (function (item, i) {
			cloned_prod = item.clone();
			cloned_prod.inject (item, 'before');	
	
			var x, y;
			var cloned;
					
			new Drag.Move (cloned_prod, {
				droppables: dropElement,
				
				onDrop : function (el, dra) {
					el.setStyle ('border', '0px solid #444444');
					el.setStyle ('left', x);
					el.setStyle ('top', y);
					el.setStyle ('opacity', 1);					
					if (!dra) { 
					}	
					else {
						cart.addToCart (id);
					}
									
				},
				
				onStart: function (el) { 

					el.setStyle ('opacity', .5);
					el.setStyle ('border', '1px solid #444444');
					x = cloned_prod.getStyle('left');
					y = cloned_prod.getStyle('top');
				}
			})
		}) */
	}
})

