var contents_opened = false;
var active_box_id = 0;

$(document).ready(function() {
	deactivate_white_boxes();
	
	$('.white_box ul li a').click(function() {
		open_page($(this).attr('pageid'), $(this).parents('.white_box').attr('boxid'));
		return false;
	});
	
	$('.white_box .orderbtn').click(function() {
		open_page('order', $(this).parents('.white_box').attr('boxid'));
		return false;
	});
	
	if (location.hash.substr(0, 5) == '#page') {
		hash = location.hash.substr(5);
		hash_arr = hash.split(',');
		open_page(hash_arr[0], hash_arr[1]);
	}
});

function send_form(obj) {
	$('#page_loading').show();
	$('#contents #contents_text #error_bar, #contents #contents_text #success_bar').hide();
	$.ajax({
	  type: 'POST',
	  url: './services/send_order.service.php',
	  data: $(obj).serialize(),
	  success: function(result) {
	  	$('#page_loading').hide();
			if (result.status == 'error') {
				$('#contents #contents_text #error_bar').html(result.error).show('slow');
			} else {
				$('#contents #contents_text #success_bar').html(result.msg).show('slow');
				$('#contents #contents_text #order_area').hide('slow');
			}
		},
	  dataType: 'json'
	});
}

function open_page(pageid, boxid, defaultname, defaultmobile, defaultemail) {	
	$('#page_loading').show();
	
	if (pageid == 'order') {
		pageurl = './services/order.service.php';
		pagedata = 'sid='+ boxid + (defaultname != '' ? '&defaultname='+ defaultname : '') + (defaultmobile != '' ? '&defaultmobile='+ defaultmobile : '') + (defaultemail != '' ? '&defaultemail='+ defaultemail : '');
	} else {
		pageurl = './services/get_page.service.php';
		pagedata = 'pid='+ pageid;
	}
	
	$.ajax({
	  type: 'POST',
	  url: pageurl,
	  data: pagedata,
	  success: function(results) {
	  	$('#page_loading').hide();
			$('#contents_text').html(results);
			activate_white_box(boxid);
		},
	  dataType: 'html'
	});
}

function deactivate_white_boxes() {
	$('.white_box').removeClass('active');
	$('#contents').hide();
	contents_opened = false;
}

function activate_white_box(boxid) {
	if (boxid == active_box_id) {
		$('html, body').animate({
			scrollTop: $("#white_boxes").offset().top
		}, 2000);
		return false;
	}
	
	$('#contents').removeClass().addClass('title'+ boxid);
		
	if (!contents_opened) {
		$('#contents').queue(function() {
			//$('#flash_intro').slideUp('slow');
			$(this).slideDown('slow');
			$(this).dequeue();
		});

		contents_opened = true;
	} else {
		$('.white_box .active_indicator').slideUp('slow');
	}
	
	$('.white_box[boxid="'+ boxid +'"] .active_indicator').slideDown('slow');
	
	if (!$('.white_box[boxid="'+ boxid +'"]').hasClass('active')) {
		$('.white_box').removeClass('active');
		$('.white_box[boxid="'+ boxid +'"]').addClass('active');
	}
	
	$('#contents').queue(function() {
		$('html, body').animate({
			scrollTop: $("#white_boxes").offset().top
		}, 2000);
		$(this).dequeue();
	});
	
	active_box_id = boxid;
}
