window.addEvent('domready', function(){ 
	new Fx.Accordion($('accordion'), '#accordion .inButton', '#accordion .subMenuMore',{
		alwaysHide: true,
		initialDisplayFx: false,
		display: -1
	});
	var toolTips = new Tips($$('.Tips'), {
             initialize: function() { this.tip.set('tween', { duration: 600 }); this.tip.fade('hide'); },
             onShow: function(tip) { tip.fade('in');  },
             onHide: function(tip) { tip.fade('out'); }
     	});
	

});

window.addEvent('load', function(){ 
	if($('searchWordsEmpty')) {
		var fSearch = document.id('fSearch');
		new OverText($('searchWordsEmpty'));
	}	
});

function addToCart(id) {
	var myHTMLRequest = new Request.HTML({
		evalScripts: true,
		url: 'public/ajax/cart.php',
		data: {
			'id' : id
		},
	 	onFailure: function(xhr) {
	 		new Message({
				icon: "dialog-error.png",
				title: 'Błąd',
				message: 'Wystąpił błąd z połączeniem, proszę spróbować jeszcze raz.'
			}).tell();
	 	},
	 	onComplete: function(xhr) {
			new Message({
				icon: "dialog-info.png",
				title: 'Produkt dodany',
				message: 'Produkt został dodany do koszyka'
			}).say();
	 	}
	}).send();
}

var Cart = new Class({
	Implements: [Events],
	removeFromCart: function(id) {
		var myHTMLRequest = new Request.HTML({
		 	evalScripts: true,
		 	update: 'cart',
			url: 'public/ajax/cart.php',
			data: {
				del: id
			},
		 	onFailure: function(xhr) {
				new Message({
					icon: "dialog-error.png",
					title: 'Błąd',
					message: 'Wystąpił błąd z połączeniem, proszę spróbować jeszcze raz.'
				}).tell();
		 	},
			onComplete: function() {
				new Message({
					icon: "dialog-info.png",
					title: 'Produkt usunięty',
					message: 'Produkt został usunięty koszyka'
				}).say();
			}
		}).send();
	},
	showCart: function(id) {
		var myHTMLRequest = new Request.HTML({
		 	evalScripts: true,
		 	update: 'cart',
			url: 'public/ajax/cart.php',
		 	onFailure: function(xhr) {
				new Message({
					icon: "dialog-error.png",
					title: 'Błąd',
					message: 'Wystąpił błąd z połączeniem, proszę spróbować jeszcze raz.'
				}).tell();
		 	}
		}).send();
	},
	
	updateCart: function(id,key,amount) {
		var myHTMLRequest = new Request.HTML({
			evalScripts: true,
			update: 'cart',
			url: 'public/ajax/cart.php',
			data: {
				pid: id,
				key: key,
				amount: amount,
				updateCart: 1
			},
			onFailure: function(xhr) {
				olay.close();
			},
			onComplete: function() {
				new Message({
					icon: "dialog-info.png",
					title: 'Koszyk zaktualizowany',
					message: 'Liczba sztuk w koszyku została zaktualizowana'
				}).say();
			}
		}).send();

	}
});

var cart = new Cart();

var PulseFade = new Class({
      
  //implements
  Implements: [Options,Events],

  //options
  options: {
    min: 0,
    max: 1,
    duration: 200,
    times: 5
  },
  
  //initialization
  initialize: function(el,options) {
    //set options
    this.setOptions(options);
    this.element = $(el);
    this.times = 0;
  },
  
  //starts the pulse fade
  start: function(times) {
    if(!times) times = this.options.times * 2;
    this.running = 1;
    this.fireEvent('start').run(times -1);
  },
  
  //stops the pulse fade
  stop: function() {
    this.running = 0;
    this.fireEvent('stop');
  },
  
  //runs the shizzle
  run: function(times) {
    //make it happen
    var self = this;
    var to = self.element.get('opacity') == self.options.min ? self.options.max : self.options.min;
    self.fx = new Fx.Tween(self.element,{
      duration: self.options.duration / 2,
      onComplete: function() {
        self.fireEvent('tick');
        if(self.running && times)
        {
          self.run(times-1);
        }
        else
        {
          self.fireEvent('complete');
        }
      }
    }).start('opacity',to);
  }
});
  
var Overlay = new Class({
	
	Implements: [Options,Events],
	
	options:  {
		id: 'overlay',
		color: '#000',
		duration: 500,
		opacity: 0.5,
		zIndex: 5000/*,
		onClick: $empty,
		onClose: $empty,
		onHide: $empty,
		onOpen: $empty,
		onShow: $empty
		*/
	},
	
	initialize: function(container,options) {
		this.setOptions(options);
		this.container = document.id(container);
		this.overlay = new Element('div',{
			id: this.options.id,
			opacity: 0,
			styles: {
				position: 'absolute',
				background: this.options.color,
				left: 0,
				top: 0,
				'z-index': this.options.zIndex
			},
			events: {
				click: function() {
					this.fireEvent('click');
				}.bind(this)
			}
		}).inject(this.container);
		this.tween = new Fx.Tween(this.overlay,{ 
			duration: this.options.duration,
			link: 'cancel',
			property: 'opacity',
			onStart: function() {
				var size = this.container.getScrollSize();
				this.overlay.setStyles({
					width: size.x,
					height: size.y
				});
			}.bind(this),
			onComplete: function() {
				this.fireEvent(this.overlay.get('opacity') == this.options.opacity ? 'show' : 'hide');
			}.bind(this)
		});
	},
	open: function() {
		if($('rmf_animation'))
			$('rmf_animation').setStyle('visibility','hidden');
		this.fireEvent('open');
		this.tween.start(this.options.opacity);
	},
	close: function() {
		if($('rmf_animation'))
			$('rmf_animation').setStyle('visibility','visible');
		this.fireEvent('close');
		this.tween.start(0);
	}
});

function selectCartShipping(id) {
	var myHTMLRequest = new Request.HTML({
		evalScripts: true,
		method: 'post',
		url: 'public/ajax/editor/shipping.php',
		data: {
			'id' : id
		},
	 	onFailure: function(xhr) {
	 		alert('Wystąpił nieznany błąd!');
	 	},
	 	onComplete: function(xhr) {
			
	 	}
	}).send();
}

function searchAction(newCatList,r) {
			if(!r) {
				var fSearch = $('fSearch');
				for (var i = 0; i < fSearch.rodzaj.length; i++) {
					if (fSearch.rodzaj[i].checked) {
						var rodzaj = fSearch.rodzaj[i].value;
						break;
					}
				}
			} else
				rodzaj = r;
			$('fSearch').action = 'locale/pl/galeria,' + rodzaj + ',' + 0 + '.html';
		}


window.addEvent('domready', function(){
	var cookieTest = Cookie.write('cookieTest', '1', {duration: 1, domain: '.obrazyfoto.pl'});
	cookieTest = Cookie.read('cookieTest');
	if(!cookieTest) {
		//var cookieError = new Element('div', {'class': 'cookieError', 'html': 'Masz wyłączoną obsługą cookies!<br />Aby zamówić obrazy lub wydruki na płótnie w serwisie obrazyfoto.pl, musisz mieć włączoną w swojej przeglądarce obsługę ciasteczek!'});
		//cookieError.inject('main');
	}
	var browser=navigator.appName;
	var b_version=navigator.appVersion;
	var version=parseFloat(b_version);
	$$('.podm').each(function(i){
		var menu = i.getChildren('.rozw');
		i.addEvents({
			'mouseenter': function(){
				menu.show();
			},
			'mouseleave': function(){
				menu.hide();
			}
		});
	});
	var div = $('locale');
	var fx = new Fx.Morph($('localeMenu'), {duration:1000, wait:false});
	div.addEvent('click', function(){
		fx.start({
			'height': 100,
			'opacity': 1
		});
	});
 
	div.addEvent('mouseleave', function(){
		fx.start({
			'height': 0,
			'opacity': 0
		});
	});

});

var Drag2Crop = new Class({
    Extends: Drag,
    Implements:[Options,Events],
    initialize: function(picture, options) {
        this.setOptions({
            relative    :    'edytor',
            modifiers   :    { x: 'scrollLeft', y: 'scrollTop' },
            style       :    false,
            invert      :    true,
            onComplete  :    this.complete.bind(this),
            debug       :    false
        }, options);
		this.top        =     0;
        this.left       =     0;
        this.picture    =     $(picture);
        this.relative   =     $(this.options.relative).setStyles({cursor:'move'});
        this.parent(this.relative);
		this.__construct();
	this.coordinates();
    },
	__construct	:    function(){
		if(Browser.Engine.trident) document.ondragstart = function (){return false;}; // IE fix
	},
    coordinates	: function(){
	this.getCoordinates	=	this.relative.getScroll();
	this.getSize		=	this.relative.getSize();
        this.width  		=	this.relative.getSize().x.toInt();
        this.height  		=	this.relative.getSize().y.toInt();
        this.left  		=	this.getCoordinates.x.toInt();
        this.top    		=	this.getCoordinates.y.toInt();
    },
	complete    :    function(){
		this.coordinates();
		this.fireEvent("done",[this.top,this.left,this.width,this.height]);
	}
});

function uploadForm() {
	el = $('uploaderBox');
	var left = screen.width / 2 - 225;
	var fx = new Fx.Morph(el, {
		duration: 1500,
		link: 'chain',
		onComplete: function (){
			
		}
	});
	fx.start({
		left: left + 'px'
	});	
}

function changeLightBoxPage(id) {
	var y = id * 40;
	$('lightBoxPager').scrollTo(0,y);
}

zoomedImage = '';
function zoomImage(i) {
	if(!i.getCoordinates)
		return;
	var coordinates = i.getCoordinates();
	if(zoomedImage)
		zoomedImage.dispose();
	zoomedImage = i.clone().setStyles({
		position: 'absolute',
		height: 'auto',
		width: 'auto',
		border: '1px solid black',
		zIndex: '10',
		top: coordinates.top + 40
	}).inject(document.body);
	zoomedImage.set('class','zoomedImage');
	zoomedImage.setStyle('left',coordinates.left - zoomedImage.width + i.width);
}

function zoomSgImage(i) {
	if(!i.getCoordinates)
		return;
	var coordinates = i.getCoordinates();
	if(zoomedImage)
		zoomedImage.dispose();

	zoomedImage = new Element('div').setStyles({
		position: 'absolute',
		height: 'auto',
		width: 'auto',
		border: '1px solid black',
		zIndex: '10',
		top: coordinates.top 
	}).inject(document.body);
	
	image = i.clone().setStyles({
		height: 'auto',
		width: 'auto'
	}).inject(zoomedImage);
	textContainer = new Element('div').setStyles({
		textAlign: 'right',
		padding: '5px',
		background: 'black',
		opacity: 0.65,
		color: 'white',
		position: 'absolute',
		bottom: '0px',
		left: '0px',
		width: image.width - 10,
		fontSize: '14px',
		fontWeight: 'bold'
	});
	textContainer.set('text',i.alt);
	textContainer.inject(zoomedImage);
	
	zoomedImage.set('class','zoomedImage');
	zoomedImage.setStyle('left',coordinates.left - image.width + i.width);
	zoomedImage.setStyle('top',coordinates.top - image.height/2 + 44);
	zoomedImage.addEvent("mouseleave",function() {
		unZoom();
	});
}

function unZoom() {
	if(zoomedImage)
		zoomedImage.dispose();
}

function logout() {
	new Message({
	icon: "dialog-question.png",
	title: "Na pewno chcesz się wylogować?",
	message: "Wylogowanie spowoduje utratę wszystkich niezapisanych plików.",
	callback: "new URI('/locale/pl/wyloguj.html').go()"
	}).ask();
}


var needToConfirm = false;

window.onbeforeunload = confirmExit;
function confirmExit()
{if (needToConfirm) return "Trwa przesyłanie pliku - jeśli opuścisz teraz stronę, operacja zostanie przerwana."; }

