/* Personalization javascript file */



var textBody = "";
var textLength = 0;
var price = 0;
var updateFx;

function changeProduct ()
{
	$('personalize_product_name').setHTML ("Ładowanie produktów...");
	new Ajax ( baseURI + "load_products", { method: 'get', onComplete: function (response) {
		var products = Json.evaluate ( response );
		var productsSelect = new Element ( "select", {
			"id": "products_select",
			"events": {
				"change": function () {
					top.location.href = baseURI + "personalize/" + this.getValue ();
				}
			}
		} );
		var productOption = new Element ( "option" );
		productOption.setHTML ( "Wybierz produkt:" );
		productsSelect.adopt ( productOption );
		products.each ( function ( product ) {
			var productOption = new Element ( "option", {
				"value": product.id
			} );
			productOption.setHTML ( product.name );
			productsSelect.adopt ( productOption );
		} );
		$('personalize_product_name').setHTML ("");
		$('personalize_product_name').adopt ( productsSelect );
	} } ).request();
}

function showError ( error )
{
	$('errors').setStyle ( "display", "block" );
	$('errors').setHTML ( error );
}

function hideError ()
{
	$('errors').effect ( "opacity", { duration: 1000 } ).start (0).chain ( function () {
		$('errors').setStyle ( "display", "none" );
		$('errors').setStyle ( "opacity", 1 );
	} );
}

function addProduct ( url, e )
{

	var txts = 0;
	$$('.personalize_text').each ( function ( el ) {
		if ( el.value != "" && el.value != "Tutaj pisz" )
			txts++;
	} );
	if ( $$('.color_selected').length == 0 )
	{
		showError ( "Musisz wybrać kolor produktu!" );
		$$('.personalize_color').addEvent ( "click", function () {
			hideError ();
		} );
		new Event ( e ).stop ();
	}
	else if ( $('popisz_sie').checked && txts == 0 )
	{
		showError ( "Zaznaczyłeś: Personalizuj. Musisz wpisać tekst personalizacji!" );
		new Event ( e ).stop ();
	}
	else if ( $('zalacz_bilecik').checked && $('personalize_note').value == "" )
	{
		showError ( "Zaznaczyłeś: Załącz liścik. Musisz wpisać treść liściku!" );
		new Event ( e ).stop ();
	}
	else {
		new Event ( e ).stop ();
		
		var item_id = top.location.href.substring ( top.location.href.lastIndexOf ( "/" ) + 1 );
		if ( item_id.indexOf ( "#" ) != -1 )
			item_id = item_id.substring ( 0, item_id.indexOf ( "#" ) );
		var product = $("personalize_product").getProperty ( "rel" );
		
		var place = "";
		if ( $$(".place_selected").length > 0 )
			place = $E(".place_selected").id.substring ( $E(".place_selected").id.indexOf ( "_" ) + 1 );
		else
			place = "";

		if($('zalacz_bilecik').checked)
			var note = $("personalize_note").value;
		else
			var note = '';
		var productData = {
			"product": product,
			"color": $E(".color_selected").id.substring ( $E(".color_selected").id.indexOf ( "_" ) + 1 ),
			"note": note,
			"box": $("ozdobne_pudelko").checked
		};
		var textQueryString = "";
		$$(".place_selected").each ( function ( el ) {
			var id = el.id.substring ( el.id.indexOf ( "_" ) + 1 );
			textQueryString += "&text_" + id + "=" + encodeURIComponent ( el.getElement ( ".personalize_text" ).value );
		} );
		var remind = false;
		if ( url == "remind" )
		{
			url = "save";
			remind = true;
		}
		new Ajax ( baseURI + "personalize/" + item_id + "/" + url, { method: "post", data: Object.toQueryString ( productData ) + textQueryString, onComplete: function () {
			if ( remind == true )
				top.location.href = baseURI + "page/45/added_to_projects"
			else
				top.location.href = baseURI + "cart"
		} } ).request();
	}
}

function updatePrice ()
{
	$('personalize_sum').getElement ( "strong" ).setHTML ( price + ",00 PLN" );
	updateFx.start ( "#fff", "#faf4de" );
}

function placeCheck ( check, force )
{
	if ( check.checked && force )
		return;
	if ( force == true )
		check.checked = true;
	
	if ( check.checked )
	{
		check.getParent().addClass ( "place_selected" );
	}
	else
	{
		check.getParent().removeClass ( "place_selected" );
	}

	// Photo
	if ( check.id == "place_51_check")
	{
		slideDiv ( "personalize_photo" );
	}

	var checks = 0;
	$$('.place_check').each ( function ( el ) { if ( el.checked ) checks++ } );
	if ( checks > 1 )
	{
		if ( check.checked == true )
		{
			price += price_place;
			updatePrice ();
			check.getParent().getElement ('.place_price').setHTML ( "+" + price_place + ",00 zł" );
		}
		else
		{
			price -= price_place;
			updatePrice ();
			if ( check.getParent().getElement ('.place_price').innerHTML == "gratis!" )
				$E('.place_selected').getElement ('.place_price').setHTML ( "gratis!" );
			check.getParent().getElement ('.place_price').setHTML ( "" );
		}
	}
	else if ( checks == 1 )
	{
		if ( check.checked == false )
		{
			price -= price_place;
			updatePrice ();
			check.getParent().getElement ('.place_price').setHTML ( "" );
			$E('.place_selected').getElement ('.place_price').setHTML ( "gratis!" );
		}
		else
			check.getParent().getElement ('.place_price').setHTML ( "gratis!" );
	}
	else
		check.getParent().getElement ('.place_price').setHTML ( "" );	
}

function photoAlign ( img )
{
	img.getParent().getParent().setStyle ( "top", Math.round ( ( 120 - img.getStyle ( "height" ).toInt() ) / 2 ) + 30 + "px" );
	img.getParent().setStyle ( "height", img.getStyle ( "height" ) );
	img.getParent().getParent().setStyle ( "left", Math.round ( ( 120 - img.getStyle ( "width" ).toInt() ) / 2 ) + 230 + "px" );
	img.getParent().setStyle ( "width", img.getStyle ( "width" ) );
}

function photoCheck ( response )
{
	if ( response == "error" )
	{
		alert ( "Wystąpił błąd podczas ładowania zdjęcia!\nProsimy spróbować ponownie." );
		$('personalize_photo_add').getElement ( "img" ).src = "images/calendar.photo.jpg";
		$('personalize_photo_add').setStyle ( "top", "30px" );
		$('personalize_photo_outer').setStyle ( "height", "" );
		$('personalize_photo_add').setStyle ( "left", "230px" );
		$('personalize_photo_outer').setStyle ( "width", "" );
		return;
	}
	$('personalize_photo_add').getElement ( "img" ).addEvent ( "load", function () {
		photoAlign ( this );
	} );
	$('personalize_photo_add').getElement ( "img" ).src = "media/photos/" + response;
}

function photoSubmit ( evt )
{
	$('personalize_photo_add').getElement ( "img" ).src = "images/ajax-loader.gif";
	$('personalize_photo_add').setStyle ( "top", Math.round ( ( 120 - 66 ) / 2 ) + 30 + "px" );
	$('personalize_photo_outer').setStyle ( "height", "" );
	$('personalize_photo_add').setStyle ( "left", Math.round ( ( 120 - 66 ) / 2 ) + 230 + "px" );
	$('personalize_photo_outer').setStyle ( "width", "" );
	new Event ( evt ).stop ();
	iframeRequest ( 'photo_form', function ( response ) {} );
}

window.addEvent ( "domready", function () { 
	$$('.personalize_color').addEvent ( "click", function () {
		$$('.color_selected').removeClass ( "color_selected" );
		this.addClass ( "color_selected" );
		$$('.personalize_place').each ( function ( el ) {
			el.getElement("img").src = "../../images/szkice/szkic_" + el.id.substring ( el.id.indexOf ( "_" ) + 1 ) + "_" + this.getProperty ( "rel" ) + ".png";
		}, this );
		this.getElement ( "span" ).effect ( "color", { duration: 1500 } ).start ( "#fa0", "#fff" );
	} );

	// Update effect
	updateFx = new Fx.Style ( "personalize_sum", "background-color", { duration: 3000, wait: false } );

	// Wpisywanie tekstu na miejscach do tłoczenia

	$$('.personalize_text').addEvent ( "focus", function () {
		if ( this.value == "Tutaj pisz" )
			this.value = "";
		$('popisz_sie_length').setHTML ( this.value.length + "/" + this.getParent().getParent().getProperty("rel") );
		placeCheck ( this.getParent().getParent().getElement ( ".place_check" ), true );
	} );

	$$('.personalize_text').addEvent ( "keyup", function () {
		if ( this.value.length > this.getParent().getParent().getProperty("rel") )
		{
			this.value = this.value.substring ( 0, this.getParent().getParent().getProperty("rel") );
		}
		$('popisz_sie_length').setHTML ( this.value.length + "/" + this.getParent().getParent().getProperty("rel") );
	} );
	
	$$('.personalize_image > img').addEvent ( "click", function () {
		placeCheck ( this.getParent().getElement ( ".place_check" ), true );
		if ( this.getElement ( "input" ) != false )
			this.getElement ( "input" ).focus();
		if ( this.getElement ( "textarea" ) != false )
			this.getElement ( "textarea" ).focus();
	} );

	$$('.place_check').addEvent ( "click", function ( e ) {
		placeCheck ( this );
	} );

	// Zmiana produktu
	$('personalize_change').addEvent ( "click", changeProduct );

	// Cena za bilecik
	$('zalacz_bilecik').addEvent ( "click", function () {
		if ( this.checked )
			price += price_note;
		else 
			price -= price_note;
		updatePrice ();
	} );

	// Cena za pudełko
	if ( $('ozdobne_pudelko') != false )
	$('ozdobne_pudelko').addEvent ( "click", function () {
		if ( this.checked )
			price += price_box;
		else
			price -= price_box;
		updatePrice ();
	} );
		
	// Sliding options
	$$('.personalize_check label').addEvent ( "click", function () {
		slideDiv ( this.getParent().id.substring ( 0, this.getParent().id.length - 7 ) );
	} );
	$$('.personalize_check input').addEvent ( "click", function () {
		slideDiv ( this.getParent().id.substring ( 0, this.getParent().id.length - 7 ) )
	} );
	$$('.personalize_check').each ( function ( el ) {
		el.getElement ( "input" ).checked = false
	} );
	$$('.place_check').each ( function ( el ) {
		el.checked = false
	} );

	// Buttons
	if ( top.location.href.indexOf ( "/edit" ) === -1 )
	{
		$('button_add').addEvent ( "click", function ( e ) {
			addProduct ( "cart_add", e );
		} );
		$('button_save').addEvent ( "click", function ( e ) {
			addProduct ( "save", e );
		} );
		$('button_remind').addEvent ( "click", function ( e ) {
			addProduct ( "remind", e );
		} );
	}

	// Left icons
	$('box_letter').getElement ( "a" ).src = "#letter";
	if ( $('box_picture') )
		$('box_picture').getElement ( "a" ).src = "#picture";
	$('box_packaging').getElement ( "a" ).src = "#packaging";
	$('box_letter').addEvent ( "click", function ( e ) {
		new Event ( e ).stop ();
		$('zalacz_bilecik').checked = true;
		$('zalacz_bilecik').fireEvent ( "click" );
		slideDiv ( "personalize_bilecik" );
	} );
	if ( $('ozdobne_pudelko') != false )
		$('box_packaging').addEvent ( "click", function ( e ) {
			new Event ( e ).stop ();
			$('ozdobne_pudelko').checked = true;
			$('ozdobne_pudelko').fireEvent ( "click" );
			slideDiv ( "personalize_opakowanie_ozdobne" );
		} );
	else
		$('box_packaging').remove();
		
	if ( top.location.href.indexOf ( "#letter" ) != -1 )
	{
		$('zalacz_bilecik').checked = true;
		$('zalacz_bilecik').fireEvent ( "click" );
	}
	if ( top.location.href.indexOf ( "#packaging" ) != -1 )
	{
		$('ozdobne_pudelko').checked = true;
		$('ozdobne_pudelko').fireEvent ( "click" );
	}
	if ( top.location.href.indexOf ( "#personalize" ) != -1 )
	{
		$('popisz_sie').checked = true;
		$('popisz_sie').fireEvent ( "click" );
	}

	// Dla przychodzących z Google

	if ( top.location.href.indexOf ( "/ad" ) != -1 )
	{
		$('popisz_sie').checked = true;
		$('popisz_sie').fireEvent ( "click" );
		$('zalacz_bilecik').checked = true;
		$('zalacz_bilecik').fireEvent ( "click" );
		if ( $('ozdobne_pudelko') != false )
		{
			$('ozdobne_pudelko').checked = true;
			$('ozdobne_pudelko').fireEvent ( "click" );
		}
	}

	// Note 10 row limit
	$('personalize_note').addEvent ( "keypress", function ( e ) {
		var lines = this.value.split ( "\n" );
		var event = new Event ( e );
		if ( lines.length >= 10 && event.key == "enter" )
			event.stop ();
	} );
	$('personalize_note').addEvent ( "keyup", function ( e ) {
		var lines = this.value.split ( "\n" );
		var event = new Event ( e );
		if ( lines.length >= 10 && event.key == "enter" )
			event.stop ();
	} );

	// Add Letter from URI //
	if ( top.location.href.indexOf ( "#add-letter" ) != -1 )
	{
		var ids = top.location.href.substring ( top.location.href.indexOf ( "#" ) + 12 );
		
		new Ajax ( '/personalize/get_letter/'+ids, { method: 'get', onComplete: function ( response ) {
			$('zalacz_bilecik').checked = true;
			$('zalacz_bilecik').fireEvent ( "click" );
			$('personalize_note').value = response;
		} } ).request ();
		new Fx.Scroll ( window ).toElement ( $('zalacz_bilecik') );
	}

	// Edit //
	if ( top.location.href.indexOf ( "/edit" ) > 0 )
	{
		if ( edit_box == "true" )
		{
			$('ozdobne_pudelko').checked = true;
			$('ozdobne_pudelko').fireEvent ( "click" );
		}
		if ( edit_note != "" )
		{
			$('zalacz_bilecik').checked = true;
			$('zalacz_bilecik').fireEvent ( "click" );
			$('personalize_note').value = edit_note.replace ( /<br \/>/g, "\n" ).replace ( /<BR \/>/g, "\n" );
		}
		var personalization = false;
		$$('.personalize_text').forEach ( function ( el ) {
			if ( el.value != "Tutaj pisz" )
			{
				el.getParent().getParent().getElement ( ".place_check" ).checked = true;
				el.getParent().getParent().getElement ( ".place_check" ).fireEvent ( "click" );
				personalization = true;
			}
		} );
		if ( personalization == true )
		{
			$('popisz_sie').checked = true;
			$('popisz_sie').fireEvent ( "click" );
		}

		$("color_" + edit_color).addClass ( "color_selected" );
		
		$('button_edit').addEvent ( "click", function ( e ) {
			addProduct ( "edit", e );
		} );

		// Kalendarz
		if ( $('photo_form') != false ) {
			photoAlign ( $('personalize_photo_add').getElement ( 'img' ) );
		}
	}
	if ( $('photo_form') != false )
	{
		$('photo_form').getElement ( "input" ).addEvent ( "change", photoSubmit );
	}
} );
