// JavaScript Document

jQuery.browser.chrome = /chrome/.test( navigator.userAgent.toLowerCase() );

var isAjaxInProgress = false;
var isHistoryInited = false;
var cache_key = 0;

function onReceiveData(data) {
	$('#menu').html(data.menu);
	$('#body').html(data.content);
	document.title = data.title;
	initAjax();
	//_typeface_js.renderDocument();
	Cufon.refresh();
	isAjaxInProgress = false;
	$('html, body').scrollTop(0);
}

function loadPage(href, data, method) {
	if (typeof data == "undefined") data = {};
	if (typeof method == "undefined") method = "GET";
	
	if (isAjaxInProgress == true) $.history.load(href);
	
	if (href.indexOf('?') != -1) {
		href += '&ajax=true&cache_key=' + cache_key;
	} else {
		href += '?ajax=true&cache_key=' + cache_key;
	}
	
	GUnload();
	
	cache_key += 1;

	$.ajax({
		type: method,
		url: href,
		data: data,
		success: onReceiveData,
		dataType: 'json'
	});
}

function loadHashPage(hash) {
	hash = hash.split('?ajax=true').join('').split('&ajax=true').join('');
	if (hash == "") hash = "index.html";
	if ((isAjaxInProgress == false && isHistoryInited == true) || (isAjaxInProgress == false && isHistoryInited == false && document.location.hash != "")) {
		loadPage(hash);
	}
}

function initAjax() {
	$("#search .submit, #productsFilter .submit").hover(function(){
		$(this).css("background-position", "bottom left");
	}, function(){
		$(this).css("background-position", "top left");
	});
	
	$('a.ajax').click(function(){
		isAjaxInProgress = true;

		var $this = $(this);
		var href = $this.attr('href');
		loadPage(href);
		return false;
	});
	
	$('a.up').click(function(){
		$('html, body').animate({
			scrollTop: 0
		}, 300);
		return false;
	});
	
	$('a[rel*=lightbox]').colorbox();
	$('a[rel=photobig]').colorbox();
	$('a[rel=photobig_link]').colorbox();
	
	$('.tooltip a').tooltip({
		showURL: false,
		loadURL: true,
		fixPNG: true,
		extraClass: "tooltipbg",
		left: -118,
		track: true
	});
	
	initGoogleMaps();

	
	$('form.ajax').each(function(){
		this.old_onsubmit = this.onsubmit;
		this.onsubmit = function(){};
	});
	
	$('form.ajax').submit(function(){
		if ($.isFunction(this.old_onsubmit)) {
			if (this.old_onsubmit() === false) {
				return false;
			}
		}

		isAjaxInProgress = true;

		var $this = $(this);
		var href = $this.attr('action');
		var method = $this.attr('method');
		var data = $this.serializeArray();
		var dataPrepared = {};
		for (i in data) {
			if (data[i].name.indexOf('[]') != -1) {
				if (typeof dataPrepared[data[i].name] == "undefined") dataPrepared[data[i].name] = [];
				dataPrepared[data[i].name].push(data[i].value);
			} else {
				dataPrepared[data[i].name] = data[i].value;
			}
		}
		if (method.toLowerCase() == 'post') {
			method = "POST";
		} else {
			method = "GET";
		}
		
		loadPage(href, dataPrepared, method);

		return false;
	});
	
	if (jQuery.browser.chrome) {
		window.onhashchange = function() {
			var h = location.hash;
			loadHashPage(h.replace(/^#/, ''));
		}
	}
}

$(document).ready(function(){
	initAjax();
	$.history.init(loadHashPage);
});
