var ajaxroot = '/';
var ajaxtimeout = 20000;
var order_id = 0;

$(document).ready(function() {
	//$.trackPage('UA-21994308-3');
	
	$('.menu').show();
	$('input:button, input:submit, button').button();
	
	$('span#contact-email').html('<a href="mailto:central@studentprint.ro">central@studentprint.ro</a>');
	
	$('#main-content').hrzAccordion({
		fixedWidth: '714',
		openOnLoad : '5',
		handleClass : 'handle',
		handlePositionArray : 'left,left,left,left,left,left'
	});
	
	var contentTop = Math.round(($(document).height() - 600) / 2);
	if (contentTop < 82) {
		contentTop = 82;
	}
	$('#main-content').css({'position': 'absolute', 'top': contentTop});
	
	var headerTop = contentTop - 82;
	if (headerTop <= 5) {
		headerTop = 5;
	}
	$('#header').css({'position': 'absolute', 'top': headerTop});
	
	var footerTop = contentTop + 600 + 10;
	$('#footer').css({'position': 'absolute', 'top': footerTop});
	
	$('#file').uploadify({
		expressInstall: ajaxroot + 'media/flash/expressInstall.swf',
		uploader: ajaxroot + 'media/flash/uploadify.swf',
		wmode: 'transparent',
		buttonText: 'Browse...',
		script: ajaxroot + 'ajax/uploadfile',
		fileDataName: 'file',
		cancelImg: ajaxroot + 'media/images/cancel.png',
		folder: 'media/files',
		queueID: 'file-queue',
		sizeLimit: 2097152000,
		auto: false,
		multi: true,
		removeCompleted: true,
		onAllComplete: function () {
			finishOrder();
		}
	});
	
	$('.accordion').accordion({
		fillSpace: true
	});
	
	$('.menu a').each(function (index) {
		$(this).click(function () {
			$('#main-contentHandle' + index).click();
			return false;
		});
	});
	
	showGoogleMaps('#map_canvas', 44.44672, 26.08736);
});

function getClientDetails () {
	if ($('#phone').val() != '') {
		$.getJSON(ajaxroot + 'ajax/getclientdetails/' + $('#phone').val(), function (data) {
			if (data != '') {
				$('#name').val(data.name);
				$('#email').val(data.email);
				$('#invoice').val(data.invoice);
			}
		});
	}
}

function sendOrder (order_type) {
	var validated = true;
	var msg = '';
	
	if ($('#phone').val() == '') {
		validated = false;
		msg += 'Numarul de telefon este obligatoriu!<br />';
	}
	
	if ($('#file-queue').html() == '') {
		validated = false;
		msg += 'Trebuie sa atasezi minim un fisier.<br />';
	}
	
	/*if ($('#name').val() == '') {
		validated = false;
		msg += 'Numele este obligatoriu!<br />';
	}
	
	if ($('#email').val() == '') {
		validated = false;
		msg += 'Adresa de email este obligatorie!<br />';
	}*/
	
	if (validated) {
		$.ajax({
			type : 'POST',
			url : ajaxroot + 'ajax/addorder',
			data: $('#form_order').serialize() + '&order_type=' + order_type,
			dataType : 'json',
			async: false,
			cache : 'false',
			timeout : ajaxtimeout,
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				ajaxError(textStatus);
			},
			success : function(json) {
				order_id = json.order_id;
				var data = {
					'order_id': json.order_id
				};
				$('#file').uploadifySettings('scriptData', data);
				$('#file').uploadifyUpload();
			}
		});
	} else {
		displayError(msg);
	}
}

function finishOrder () {
	$('#file').uploadifyClearQueue();
	$('#discount').val('');
	$('#pages').val('');
	$('#copies').val('1');
	$('#messagedlg')
		.html('Comanda cu nr. ' + order_id + ' a fost adaugata.')
		.dialog({
			title: 'Info',
			buttons: {
				'Adauga comanda': function() {
					$(this).dialog('close');
				},
				'Urmareste comanda': function () {
					popupTrackOrder();
					trackOrderNumber(order_id);
					$(this).dialog('close');
				}
			},
			width: 'auto',
			height: 'auto',
			modal: true,
			resizable: false
		});
	
	$.ajax({
		type : 'POST',
		url : ajaxroot + 'ajax/sendordermail',
		data: 'order_id=' + order_id,
		dataType : 'json',
		async: false,
		cache : 'false',
		timeout : ajaxtimeout,
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			ajaxError(textStatus);
		},
		success : function(json) {
			
		}
	});
}

function trackInputKeyPress (e) {
	if (e.keyCode == 13) {
		trackOrder();
		$('#order_phone').select();
	}
}

function popupTrackOrder () {
	$('#order-tracking')
		.dialog({
			title: 'Urmarire Comanda',
			buttons: {
				'Inchide': function() {
					$(this).dialog('close');
				}
			},
			width: 600,
			height: 450,
			modal: true,
			resizable: false
		});
	$('#order-tracking').dialog('open');
}

function trackOrder () {
	if ($('#order_phone').val() != '') {
		trackOrdersPhone($('#order_phone').val(), $('#order_password').val());
	} else {
		$('#order-tracking-result').html('Trebuie sa introduci numarul de telefon.');
	}
}

function trackOrdersPhone (phone, password) {
	$.ajax({
		type : 'POST',
		url : ajaxroot + 'ajax/trackordersphone',
		data: 'phone=' + phone + '&password=' + password,
		dataType : 'text',
		async: false,
		cache : 'false',
		timeout : ajaxtimeout,
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			ajaxError(textStatus);
		},
		success : function(text) {
			$('#order-tracking-result').html(text);
		}
	});
}

function trackOrderNumber (order_id) {
	$.ajax({
		type : 'POST',
		url : ajaxroot + 'ajax/trackorder',
		data: 'order_id=' + order_id,
		dataType : 'text',
		async: false,
		cache : 'false',
		timeout : ajaxtimeout,
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			ajaxError(textStatus);
		},
		success : function(text) {
			$('#order-tracking-result').html(text);
		}
	});
}

function showContactForm () {
	$('#contactform')
		.dialog({
			title: 'Formular Contact',
			buttons: {
				'Trimite': function() {
					var validated = true;
					
					var msg = '';
					
					if ($('#contactform #contact_name').val() == '') {
						validated = false;
						msg += 'Campul "Nume" este obligatoriu.<br />';
					}
					
					if ($('#contactform #contact_text').val() == '') {
						validated = false;
						msg += 'Campul "Mesaj" este obligatoriu.<br />';
					}
					
					if ($('#contactform #contact_phone').val() == '' && $('#contactform #contact_email').val() == '') {
						validated = false;
						msg += 'Trebuie sa completati numarul de telefon sau adresa de email.';
					}
					
					if (validated) {
						submitContactForm();
						$(this).dialog('close');
					} else {
						displayNotification('middle center', 2000, msg);
					}
				},
				'Renunta': function() {
					$(this).dialog('close');
				}
			},
			width: 'auto',
			height: 'auto',
			modal: true,
			resizable: false
		});
	$('#contactform').dialog('open');
}

function submitContactForm () {
	$.ajax({
		type : 'POST',
		url : ajaxroot + 'ajax/sendmessage',
		data: 'name=' + $('#contactform #contact_name').val() + '&phone=' + $('#contactform #contact_phone').val() + '&email=' + $('#contactform #contact_email').val() + '&text=' + $('#contactform #contact_text').val(),
		dataType : 'json',
		async: false,
		cache : 'false',
		timeout : ajaxtimeout,
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			ajaxError(textStatus);
		},
		success : function(json) {
			if (json.sent) var msg = 'Mesajul a fost trimis.';
			else var msg = 'Mesajul nu a putut fi trimis.';
			displayNotification('middle center', 3000, msg);
			$('#contactform #phone').val('');
			$('#contactform #email').val('');
			$('#contactform #text').val('');
		}
	});
}

function showComplaintForm () {
	$('#contactform')
		.dialog({
			title: 'Formular Contact',
			buttons: {
				'Trimite': function() {
					var validated = true;
					
					var msg = '';
					
					if ($('#contactform #contact_name').val() == '') {
						validated = false;
						msg += 'Campul "Nume" este obligatoriu.<br />';
					}
					
					if ($('#contactform #contact_text').val() == '') {
						validated = false;
						msg += 'Campul "Mesaj" este obligatoriu.<br />';
					}
					
					if ($('#contactform #contact_phone').val() == '' && $('#contactform #contact_email').val() == '') {
						validated = false;
						msg += 'Trebuie sa completati numarul de telefon sau adresa de email.';
					}
					
					if (validated) {
						submitComplaintForm();
						$(this).dialog('close');
					} else {
						displayNotification('middle center', 2000, msg);
					}
				},
				'Renunta': function() {
					$(this).dialog('close');
				}
			},
			width: 'auto',
			height: 'auto',
			modal: true,
			resizable: false
		});
	$('#contactform').dialog('open');
}

function submitComplaintForm () {
	$.ajax({
		type : 'POST',
		url : ajaxroot + 'ajax/sendcomplaintmessage',
		data: 'name=' + $('#contactform #contact_name').val() + '&phone=' + $('#contactform #contact_phone').val() + '&email=' + $('#contactform #contact_email').val() + '&text=' + $('#contactform #contact_text').val(),
		dataType : 'json',
		async: false,
		cache : 'false',
		timeout : ajaxtimeout,
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			ajaxError(textStatus);
		},
		success : function(json) {
			if (json.sent) var msg = 'Mesajul a fost trimis.';
			else var msg = 'Mesajul nu a putut fi trimis.';
			displayNotification('middle center', 3000, msg);
			$('#contactform #phone').val('');
			$('#contactform #email').val('');
			$('#contactform #text').val('');
		}
	});
}

function popupFooter (page) {
	$.ajax({
		type : 'POST',
		url : ajaxroot + 'ajax/getpage',
		data: 'page=' + page,
		dataType : 'text',
		async: false,
		cache : 'false',
		timeout : ajaxtimeout,
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			ajaxError(textStatus);
		},
		success : function(text) {
			$('#messagedlg').html(text)
				.dialog({
					title: 'Info',
					buttons: {
						'Inchide': function() {
							$(this).dialog('close');
						}
					},
					width: 960,
					height: 600,
					modal: true,
					resizable: false
				});
			$('#messagedlg').dialog('open');
		}
	});
}
