// Base functions for the shop

var baseURI = "http://www.galopersona.pl/";
var vars = [];

function doSearch ()
{
	top.location.href = baseURI + "search/" + $('searchbox').getValue().toLowerCase().replace ( / /, "_" );
}

function carouselRun ( num )
{
	num++
	if ( num > $$('#top_images img').length )
	{
		return false;
	}
	$('banner_' + num).effect ( "opacity", { duration: 1000 } ).start ( 1 );
	( function () { carouselRun ( num ); } ).delay ( 6000 );
}

function initCarousel ()
{
	var i = 0;
	this.forEach ( function ( img ) {
		i++;
		var image = new Element ( "img", {
			id: "banner_" + i,
			src: img,
			styles: {
				opacity: 0,
				"z-index": i,
				visibility: "visible",
				position: "absolute",
				top: 0,
				left: 0
			}
		} );
		//ukryte poniewaz slajd jest zdjety       $('top_images').adopt ( image );
	} );
	carouselRun ( 0 );
}

function initLogin ()
{
	if ( $('login') != false && $('login') != null )
	{
		$$('#login input').each ( function ( input ) {
			if ( input.name == "email" || input.name == "password" )
			{
				input.addEvent ( "focus", function () {
					if ( this.name == "email" && this.value == "adres e-mail" )
						this.value = "";
					if ( this.name == "password" && this.value == "password" )
						this.value = "";
					this.setStyle ( "color", "#000" );
				} );
			}
		} );
	}
}

function initDOM () {
	var imgs = ['images/banner.top.1.jpg', 'images/banner.top.4.jpg', 'images/banner.top.2.jpg', 'images/banner.top.3.jpg'];
	new Asset.images ( imgs, { onComplete: initCarousel.bind ( imgs ) } );
	$$('a').each ( function ( a ) {
		if ( a.getProperty ( "rel" ) == "external" )
			a.addEvent ( "click", function ( e ) {
				new Event ( e ).stop ();
				window.open ( this.href );
			} );
	} );
	$('searchbox').addEvent ( "keyup", function ( e ) {
		e = new Event ( e );
		if ( e.key == "enter" )
			doSearch();
	} );

	// Widok produktu - zmiana linków w bannerach po lewej
	if ( top.location.href.indexOf ( "/product/" ) !== -1 )
	{
		var product_id = top.location.href.substr ( top.location.href.lastIndexOf ( "/" ) + 1 );
		if ( product_id != 27 )
			$('box_picture').remove();
		$('box_personalize').getElement ( "a" ).href = "/personalize/" + product_id + "#personalize";
		$('box_packaging').getElement ( "a" ).href = "/personalize/" + product_id + "#packaging";
		$('box_letter').getElement ( "a" ).href = "/personalize/" + product_id + "#letter";
	}
	
	initLogin();
}

function slideDiv ( obj, execute )
{
	var div = $(obj);
	var divHeight = div.getStyle( 'height' ).toInt();
	var divDisplay = div.getStyle ( 'display' );
	if ( divHeight <= 1 || divDisplay != "block" )
	{
		div.setStyles ({
			opacity: 0,
			height: 0,
			display: 'block',
			overflow: 'hidden'
		});
		var effect = div.effects({
			duration: 500,
			transition: Fx.Transitions.sineInOut,
			onComplete:  function() { div.setStyle('height',''); if ( execute != undefined ) { execute(); } }
			});
		effect.custom({
			'height': [0, div.scrollHeight],
			'opacity': [0, 1]
		});
	}
	else
	{
		div.setStyle ( 'height', div.scrollHeight + 'px' );
		var effect = div.effects({
			duration: 300,
			transition: Fx.Transitions.sineInOut,
			onComplete:  function() { if ( execute != undefined ) { execute(); } }
			});
		effect.custom({
			height: [div.scrollHeight, 0],
			opacity: [1,0]
			});
	}
}

function slideDivs ( togglers, divs )
{
	togglers.each ( function (toggler,i) {
		if ( $(toggler) !== false )
		{
			$(toggler).addEvent ( "click", function() { slideDiv ( divs[i] ) } );
			$(divs[i]).setStyles({'opacity': 0, 'height': 0, 'overflow': 'hidden'});
		}
	});
}

function iframeRequestDo ()
{
	this.target = this.id + "_frame";
	this.submit();
}

function iframeRequest ( formID, callback )
{
	var transportFrame;
	if ( $('formID + "_frame"') == false )
	{
		transportFrame = new Element ( "iframe" );
		transportFrame.injectInside ( document.body );
	}
	else
		transportFrame = $('formID + "_frame"');
		
	transportFrame.id = formID + "_frame";
	transportFrame.name = formID + "_frame";
	transportFrame.style.display = "none";
	iframeRequestDo.delay(100, $(formID));
}

window.addEvent ( "domready", initDOM );

