var base_url = "http://"+location.hostname+"/";
var root_dir = "http://"+location.hostname+"/index.php/";
var secure_root = "http://"+location.hostname+"/index.php/";
var language = 'english';
var theme = 'freestart';
var asset_path = base_url+'system/assets/default/';

$(document).ready(function() {
	$('#product_search').keypress(function(e) {
		if(e.keyCode == 13) {
			doSearch();
		}
	});
	
	$('#buegle-security-banner').click(function(e) {
		e.preventDefault();
		$.colorbox({
			href	:	$(this).attr('href'),
			iframe	:	true,
			innerWidth	:	450,
			innerHeight	:	470,
			close		:	'close'
		});
	});
	
	$('.tip').tipTip();
	$('.tip-left').tipTip({defaultPosition:'left'});
	$('.tip-right').tipTip({defaultPosition:'right'});
});

function ajaxPostRequest(file, command, params, do_function, callback_params, fail_function) {
	$.ajax({
	  "data": params,
	  "url": root_dir+file+"/"+command,
	  "type": "POST",
	  "timeout": "45000",
	  "cache": false,
	  "beforeSend": function(){
		$('#working').show();
	  },
	  "success": function(html){
		do_function(html, callback_params);
	  },
	  "complete": function(transport){
		$('#working').hide();
	  },
	  "error": function() {
		if (fail_function) {
			fail_function();
		}
	  }
	});
}

function doSearch() {
	var term = $('#product_search').val();
	var params = 'term='+term;

	$('#do_product_search').val('Searching...');
	$('#do_product_search').attr('disabled', 'disabled');

	ajaxPostRequest('ajax', 'product_search', params, response);

	function response(html) {
		$('#do_product_search').val('Search');
		$('#do_product_search').removeAttr('disabled');

		$.colorbox({
			href: root_dir+'ajax/search_results/'+html,
			iframe: true,
			width: '80%',
			height: '80%',
			overlayClose: true,
			opacity: 0.5,
			close: 'Close',
			onOpen: function(){
				$('#cboxClose').css('visibility', 'visible');
				$().unbind("keydown.cbox_close");
			},
			onClosed: function() {
				//
			}
		});
	}
}
