Effect.DefaultOptions.duration = .5;

function flash_it()
{
	var f = $('flash');
	if(f) {
		// move flash to the body
		document.body.insert(f);
		var h = parseInt(document.viewport.getHeight()/2);
		var o = document.viewport.getScrollOffsets();
		h = (h + o[1] - 20) + 'px';
		f.setStyle({top: h});
		var img;
		if(img = f.down('img')) {
			img.observe('click', function(i){
				if($('flash_close_link')) {
					document.location = $('flash_close_link').innerHTML; 
				} else {
					new Effect.Fade(f)
				}
			});
		} else {
			new PeriodicalExecuter(function(ev) {
				ev.stop();
				new Effect.Fade(f)
			}, 3);
		}
		new Effect.Appear(f);
    }
}

function banner(id, url, time)
{
	var duration = 2.0;
	if(!$(id).swap) {
		$(id).swap = function() {
			new Effect.Fade(this.childElements().first(), {
				afterFinish:function(obj){obj.element.remove()},
				duration: duration
			});
			new Effect.Appear(this.childElements().first().next(), {
				duration: duration
			});
		}
	}
	if(!$(id).wipe) {
		$(id).wipe = function() {
			this.childElements().first().next().remove();
		}
	}
	
	if(!$(id).preload) {
		$(id).preload = function() {
			var images = this.childElements().last().select('img');
			var total = images.length
			if(total > 0) {
				images.each(function(img){
					var i = new Element('img');
					i.onload=function() {
						if(!--total) {
							$(id).swap()
						}
					}
					i.onerror=function() {
						if(!--total) {
							$(id).swap()
						}
					}
					i.src=img.src
				})
			} else
				this.swap();
		}
	}
	
	new PeriodicalExecuter(function(e){
		e.stop();
		new Ajax.Request(url, {method: 'post', parameters: {jid: id}, onSuccess: function(t) {
			new Insertion.Bottom($(id), t.responseText);
			$(id).preload()
		}})
	}, time)
}

function closeSubWindowByKeypress(e)
{
	if(e.keyCode == 27)
		$('subwindow').fire('or:close');
}

function closeSubWindowByButtonPress(e)
{
	$('subwindow').fire('or:close');
}

document.observe('dom:loaded', function(){
	function albumBoxToggle(e) {
		var box = e.findElement('.album-box');
		box.toggleClassName('active-album-box');
	};
	
	$$('.album-box').invoke('observe', 'mouseover', albumBoxToggle);
	$$('.album-box').invoke('observe', 'mouseout', albumBoxToggle);
	
	$$('#newsletter-email').invoke('observe', 'focus', function(e) {
		e = e.findElement('input');
		if(!e.fs) {
			e.fs = true;
			e.value = '';
		}
	});
	
    if(ul = $$('ul.images-scroller').first()) {
        ul.select('a').invoke('observe', 'click', function(a) {
            a.findElement('ul').up().down('img.artist-image').writeAttribute('src', a.findElement('a').href);
            a.stop();
        });
    }
    $$('form.price-block').each(function(f){
        f.observe('or:submit', function(e){
            var form = e.findElement('form')
            new Ajax.Updater($('basket').down('span'), form.action, {parameters:Form.serialize(form)})
            e.stop();
        })
        f.observe('submit', function(e){e.findElement('form').fire('or:submit'); e.stop(); })
    })
    $$('a.buy-link').invoke('observe', 'click', function(e){e.findElement('form').fire('or:submit'); e.stop(); })
    $$('.arrow-down, .arrow-up').invoke('observe', 'click', function(e){
	        Effect.toggle($(e.element().id+'-additional'), 'blind', {queue: 'end'});
   	    	e.element().toggleClassName('arrow-up');
    })
    $$('a.basket-delete').invoke('observe', 'click', function(e){
    	if(confirm(confirm_text)) {
	    	var a = e.element();
	    	a.addClassName('ajax');
	    	new Ajax.Request(a.href, {
	    		evalJS: 'force',
	    		onSuccess: function(t){
		    		var data = t.responseText.split('/');
		    		if(data.length == 2) {
			    		$$('span.total-price').each(function(e){e.innerHTML = data[1]});
			    		$$('span.total-quantity').each(function(e){e.innerHTML = data[0]})
			    		new Effect.Fade(a.up('tr'));
		    		}
		    	}
	    	})
    	}
    	e.stop();
    })
    flash_it();
    var flash;
    if(flash = $('jump-to-friends')) {
    	flash.observe('change', function(e){
    		var s = e.findElement('select');
    		if(s.selectedIndex > 0){
    			window.open(s.options[s.selectedIndex].value);
    		}
    	});
    }
    $$('a.subwindow').invoke('observe', 'click', function(a){
    	a.stop();
    	a = a.findElement('a');
    	new Ajax.Updater($('subwindow'), a.href, {
    		onComplete: function(transport)
    		{
    			var i;
    			if(i = $('subwindow').down('img')) {
    				i.observe('click', function(e){
    					$('subwindow').fire('or:close');
    				})
    			}
    			Element.fire(document, 'or:resize', true);
    			new Effect.Appear('subwindow-overlay', {queue: 'end'})
    			new Effect.Appear('subwindow', {queue: 'end'})
    			Event.observe(document, 'keydown', closeSubWindowByKeypress);
    		}
    	})
    })
    Event.observe(document, 'or:resize', function(e){
    	var subwindow = $('subwindow'); 
    	if(subwindow.visible() || e.memo) {
		if($('subwindow-content')) {
			var arrayPageScroll = document.viewport.getScrollOffsets();
			var lightboxTop = parseInt(arrayPageScroll[1] + (document.viewport.getHeight() / 6.67));
			Element.setStyle(subwindow, { top: lightboxTop + 'px'});
			Element.setStyle(subwindow, {height: (document.viewport.getHeight()*0.7) + 'px'});
			var h = $$('body').first().getHeight();
			if(h < document.viewport.getHeight())
				h = document.viewport.getHeight();
			Element.setStyle($('subwindow-overlay'), {height: h+'px', width: document.viewport.getWidth()+'px'});
			Element.setStyle($('subwindow-content'), {
			    width: $('subwindow').getWidth()-32+'px',
			    height: $('subwindow').getHeight()-62+'px'
			})
			if(subwindow.visible())
				$$('select').invoke('hide');
                }
    	}
    })
	$$('#subwindow').invoke('observe', 'or:close', function(e){
		var subwindow = $('subwindow');
		if(subwindow && subwindow.visible()) {
			var i;
			if(i = subwindow.down('img')) {
				i.stopObserving('click', closeSubWindowByButtonPress);
			}
			Event.stopObserving(document, 'keydown', closeSubWindowByKeypress);
			Effect.Fade('subwindow', {queue: 'end'});
			Effect.Fade('subwindow-overlay', {queue: 'end'});
			new PeriodicalExecuter(function(e){$('subwindow').innerHTML=''; e.stop()}, 1);
			$$('select').invoke('show');
		}
	})
    Event.observe(window, 'resize', function() {
    	Element.fire(document, 'or:resize');
    })
    Event.observe(window, 'scroll', function() {
    	Element.fire(document, 'or:resize');
    })
    
})


