/* Initial variables used for the price selector */
var currentSize = "6x4";
var currentFinish = 0;
var currentReplaceDisplayEl;
var currentReplaceSizeEl;
var currentReplaceFinishEl;
var currentCountValue = 0;
window.addEvent('load', function() {
	if($('form-upload')){
	// For testing, showing the user the current Flash version.
	// document.getElement('p.messages').appendText(' Detected Flash ' + Browser.Plugins.Flash.version + '!');
	var swiffy = new FancyUpload2($('upload-status'), $('upload-list'), {
		url: $('form-upload').action,
		fieldName: 'photoupload',
		path: '/assets/FancyUpload/Swiff.Uploader.swf',// Change me to absolute
		limitSize: 200 * 1024 * 1024, // 30Mb
		onLoad: function() {
			
	
			$('upload-status').removeClass('hide');
			$('upload-lists').removeClass('hide');
			$('upload-fallback').destroy();
			window.fireEvent('resize');
		},
		// The changed parts!
		debug: true, // enable logs, uses console.log
		target: 'upload-browse', // the element for the overlay (Flash 10 only)
		allowDuplicates: true
	});

	swiffy.options.typeFilter = {'Images or Zip (*.jpg, *.jpeg, *.gif, *.png, *.zip, *.tiff, *.tif, *.pdf, *.eps, *.bmp, *.psd)': '*.jpg;*.jpeg;*.gif;*.png;*.zip;*.tiff;*.tif;*.pdf;*.eps;*.bmp;*.psd'};

	/**
	 * Various interactions
	 */

	$('upload-browse').addEvent('click', function() {
		/**
		 * Doesn't work anymore with Flash 10: swiffy.browse();
		 * FancyUpload moves the Flash movie as overlay over the link.
		 * (see opeion "target" above)
		 */
		swiffy.browse();
		return false;
	});

	$('upload-clear').addEvent('click', function() {
		swiffy.removeFile();
		$$('.file-remove').each(function(el){
			el.getParent('li').destroy();
		});
		return false;
	});

	$('upload-upload').addEvent('click', function() {
		swiffy.upload();
		return false;
	});

	/*$('size_select_all').addEvent('change', function() {
		var si = $('size_select_all').selectedIndex;
		$$('.photosizes_select').each(function(el) {
			el.selectedIndex = si;
		});
	});*/

	$('qty_select_all').addEvent('change', function() {
		var si = $('qty_select_all').value;
		$$('.photoqty_select').each(function(el) {
			el.value = si;
		});
	});

	$$('.file-remove').each(function(el){
		el.addEvent('click', function() {
			// remove this list item
			this.getParent('li').destroy();
			if (!$('upload-list').hasChild('li')) $('no-image').removeClass('hide');
			return false;
		}.bind(el))
	});
	
	$$('.file-size-remove').each(function(el){
		el.addEvent('click', function() {
			// remove this list item
			this.getParent('.a-size').destroy();
			return false;
		}.bind(el))
	});
	
	$$('.file-size-display').each(function(el) {
		var i = 0;
		//el.set('rel', i);
		el.addEvent('click', function() {
//			setPhotoSize('file-size-display-'+this.rel,
//				 'file-size-hidden-'+this.rel, 
//				 'file-size-finish-'+this.rel
//			);
			
			setPhotoSize('file-size-display-'+this.get('rel'),
					 'file-size-hidden-'+this.get('rel'), 
					 'file-size-finish-'+this.get('rel')
				);
			
		}.bind(el));
		i++;
		currentCountValue++;
	});
	$$('.preventLeave').each(function(el) {
		el.addEvent('click', function() {
			if(swiffy.files.length != 0){
				var incomplete = false;
				for(i=0;i<swiffy.files.length;i++){
					if(swiffy.files[i].finished != true){
						incomplete = true;
					}
				}
				if(incomplete == true){
					return confirm("You have selected files to upload, but have not yet uploaded them. Click 'cancel' to continue with your order, or 'Ok' to leave your order");
				}else{
					return confirm("To continue with your order, you must enter your order details and click next. Click 'cancel' to continue with your order, or 'Ok' to leave your order");
				}
			}
			
		});
	});
	$$('.nextPrevent').each(function(el) {
		el.addEvent('click', function() {
			if(swiffy.files.length != 0){
				var incomplete = false;
				for(i=0;i<swiffy.files.length;i++){
					if(swiffy.files[i].finished != true){
						incomplete = true;
					}
				}
				if(incomplete == true){
					alert("You have selected files to upload, but have not uploaded them. Please click the 'upload' button before proceeding.");
					return false;
				}
			}
			
		});
	});
	var filecount = 0;
	$$('.file-size-add').each(function(el){
		filename = $('file_name_'+filecount).value;
		filecount++;
		
		el.addEvent('click', function() {
			var photosize = new Element('a',{
				'class': 'file-size-display',
				'id' : 'file-size-display-' + currentCountValue,
				'rel' : currentCountValue,
				'html': " " + $('setSizeMain').innerHTML + " ",
				'href': '#',
				'events': {
					'click': function() {
						setPhotoSize('file-size-display-' + this.rel,
							 'file-size-hidden-' + this.rel, 
							 'file-size-finish-' + this.rel
						);
						return false;
					}
				}
	
			});//var photosize = firstsizes.clone();
			// hidden photo size element
			var thisParent = this.getParent().getChildren('.a-size').getChildren('.photoqty_select');
			var curFileName = thisParent[0].get('name');
			var curFileString = new String();
			curFileString = curFileName.toString();
			curFileName = curFileString.split('[');
			curFileName = curFileName[1].split(']');
			curFileName = curFileName[0];
			var photohiddensize = new Element('input',{
				'type': 'hidden',
				'id' : 'file-size-hidden-' + currentCountValue,
				'name': 'file_photosizes['+curFileName+'][]',
				'value': $('size_select_all').get('value')			});
			// hidden photo finish element
			var photohiddenfinish = new Element('input',{
				'type': 'hidden',
				'id' : 'file-size-finish-' + currentCountValue,
				'name': 'file_photofinish['+curFileName+'][]',
				'value': $('finish_select').get('value')			});
			var photoqty = $('qty_select_all').clone();
			photoqty.set('name', 'file_photoquantity['+curFileName+'][]');
			
			//currentCountValue++;
			var removesize = new Element('a', {
				'class': 'file-size-remove',
				'html': '[ - ]',
				'href': '#',
				'events': {
					'click': function() {
						this.getParent().destroy();
						return false;
					}
				}
			});

			/* new */
			//new
			var photoformat_fit = new Element('input',{
				'type': 'radio',
				'id' : 'file-format-' + currentCountValue,
				'name': 'file_format['+curFileName+']['+currentCountValue+']',
				'value': 'fit',
				'checked' : true
			});
			
			var photoformatfittext = new Element('label', {	 
				text:'Fit'
			});
			
			var photoformat_fill = new Element('input',{
				'type': 'radio',
				'id' : 'file-format-' + currentCountValue,
				'name': 'file_format['+curFileName+']['+currentCountValue+']',
				'value': 'fill'
			});
			
			var photoformatfilltext = new Element('label', {	 
				text:'Fill'
			});
			
			var photoformat_noresize = new Element('input',{
				'type': 'radio',
				'id' : 'file-format-' + currentCountValue,
				'name': 'file_format['+curFileName+']['+currentCountValue+']',
				'value': 'no-resize'			
			});
			
			var photoformatnoresizetext = new Element('label', {	 
				text:'No Resize'
			});
			
			
			
			var firstwhiteborderyes = new Element('input',{
				'type': 'radio',
				'id' : 'white-border-' + currentCountValue,
				'name': 'white-border['+curFileName+']['+currentCountValue+']',
				'value': 'WB'			
			});
			
			var firstwhiteborderyeslabel = new Element('label', {	 
				text:'BRD'
			});
			

			var firstwhiteborderno = new Element('input',{
				'type': 'radio',
				'id' : 'white-border-' + currentCountValue,
				'name': 'white-border['+curFileName+']['+currentCountValue+']',
				'value': 'NB',
				'checked' : true
			});
			
			var firstwhitebordernolabel = new Element('label', {	 
				text:'No BRD'
			});
			
			currentCountValue++;
			
			/* end new */
			
			
			
			var a_size = new Element('div', {'class': 'a-size'}).adopt(
				photoqty,
				photosize,
				photohiddensize,
				photohiddenfinish,
				removesize,
				
				photoformat_fit,
				photoformatfittext,
				photoformat_fill,
				photoformatfilltext,
				photoformat_noresize,
				photoformatnoresizetext,
				firstwhiteborderyes,
				firstwhiteborderyeslabel,
				firstwhiteborderno,
				firstwhitebordernolabel
				
				
			);
			a_size.inject(this, 'before');
			
			return false;
		})
	});

	if($('details_name')){
		$('details_name').addEvent('blur', function () {
			if(this.value == ''){ 	this.addClass('error');
				} else { 				this.removeClass('error'); }
			});
	}
	if($('details_email')){
		$('details_email').addEvent('blur', function () {
			if ((this.value == '') || (this.value.test(/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/) == false)) {
				this.addClass('error');
			} else {
				this.removeClass('error');
			}
		});
	}
	if($('details_phone')){
		$('details_phone').addEvent('blur', function () {
			if(this.value == ''){ 	this.addClass('error');
			} else { 				this.removeClass('error'); }
		});
	}

	if ($('form-details')) {
		$('form-details').addEvent('submit', function() {
			var error = null;
			// validate not empty
			if ($('details_name').value == '') {
				el = $('details_name');
				error = 'Please enter a valid name';
			} else if ($('details_phone').value == '') {
				el = $('details_phone');
				error = 'Please enter a valid phone number';
			}else if ($('details_address').value == '') {
				el = $('details_address');
				error = 'Please enter a valid address';
			}else if ($('details_city').value == '') {
				el = $('details_city');
				error = 'Please enter a valid city';
			} else if ($('details_email').value.test(/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/) == false) {
				el = $('details_email');
				error = 'Please enter a valid email address';
			}else if($('howhear').value == ''){
				el = $('howhear');
				error = 'Please select how you heard about Ezy Prints';
			} else {
				this.submit();
			}
			if (error) {
				alert(error);
				el.addClass('error');
				return false;
			}
		});
	}
	}
});
function setPhotoSize(displayEl, sizeEl, finishEl){
	 var caption = "Select Print Size and Finish";
    // get rel attribute for image groups
    var group = false;
	currentReplaceDisplayEl = displayEl;
	currentReplaceSizeEl = sizeEl;
	currentReplaceFinishEl = finishEl;
	currentSize = "6x4";
	currentFinish = 0;
    // display the box for the elements href
    TB_show(caption, "#TB_inline?height=520&width=400&inlineId=sizesContent", group);

	return false;
}
function firePricingSizeChange(printSize){
	var newPrices = printPrices[printSize];
	currentSize = printSize;
	var i;
	var html = "";
	html = "<table class='normalTable'><tr><th style='width:120px'>Finish</th><th>Price*</th></tr>";
	for(i=0;i<newPrices.length;i++){
		html += "<tr><td>";
		if(i == 0){
			html += "<label><input type='radio' name='finishType' value='0' onChange='firePricingChange(0)' id='hs_image_finish' checked='checked' /> Lustre</label>";
		}else if(i ==1){
			html += "<label><input type='radio' name='finishType' value='1' onChange='firePricingChange(1)' id='hs_image_finish' /> Pearl</label>";
		}else if(i == 2){
			html += "<label><input type='radio' name='finishType' value='2' onChange='firePricingChange(2)' id='hs_image_finish' /> Laminated</label>";
		}
		html += "</td><td><span class='light'>$" + newPrices[i][1].toFixed(2) + "</small></td></tr>";
	}
	html += "<tr><td></td></tr><tr><td colspan='2'>&nbsp;</td></tr><tr><td colspan='2' style='text-align:center'><a href='#' onClick='return firePricingSave()';'><big><big>Click Here To Save</big></big></a></td></tr></table>";
	//alert($('pricingFinishDisplay').innerHTML);
	$('pricingFinishDisplay').innerHTML = html;
}
function firePricingChange(finishVal){
	currentFinish = finishVal;
}
function firePricingSave(){
	if(currentFinish == 1){
		currentFinishName = "Pearl";
	}else if(currentFinish == 2){
		currentFinishName = "Laminated";
	}else{	
		currentFinishName = "Lustre";
	}
	
	//alert(currentReplaceDisplayEl);
	
	$(currentReplaceDisplayEl).set('text', currentSize + " " + currentFinishName);
	$(currentReplaceSizeEl).set('value', currentSize);
	$(currentReplaceFinishEl).set('value', currentFinish);
	TB_remove();
	
	return false;

}

