$(document).ready( function(){
	
	/*var count = $('#top .menu li').length;
	var width = $('#top .menu').width();

	var cell_width = Math.round(width / count);
	
	var i = 1;
	$('#top .menu li').each( function(){
		if(i == count){
			cell_width = width;
		}
		$(this).css('width', cell_width+'px');
		i++;

		width -= cell_width;
	});*/

	/*$('div.shoe div.icons a').hover( function() {
		$(this).css('background-position', 'top');
	}, function() {
		$(this).css('background-position', 'bottom');
	});*/


	//Skicka biljett contest
	$('form#ticket').submit( function(){

		var form = $('form#ticket').formSerialize();

		$.post('handlers/ticket.handler.php', form, function(data){

			data = data.split('|');

			if(data[0] > 0){
				$('.form').hide();
				$('.confirm').show();
			}else if(data[0] == -1){
				$('.error').html(data[1]);
				$('.error').show();
			}else{
				$('.error').html(data[1]);
				$('.error').show();
			}
		});

		return false;
	});
});


/** GENERAL POPUP **/
function popup (url, name, width, height, status, menu, resizable) {
	var prop = 'toolbar=no,location=no,directories=no,scrollbars=yes,copyhistory=no,';
	prop += 'status='+ (status ? 'yes' : 'no') +',';
	prop += 'resizable='+ (resizable ? resizable : 'yes') +',';
	prop += 'menu='+ (menu ? 'yes' : 'no') +',';
	prop += 'width='+ (width ? width : 500) +',';
	prop += 'height='+ (height ? height : 400);

	var win = window.open(typeof(url) == 'string' ? url : url.href, name, prop);

	win.focus();
	
	return false;
}


/**** START FAVORITTER ****/

function addToFavo(modellId) {

	$.get('xmlhttp/favo.php', {action: 'add', modell: modellId}, function (data) {
		if (data) {
			var value = data.split("||");
			alert(value[1]);
			$('#menu_favo').html(value[0]);

			$('td.td_addToFavo, td.td_delFavo').toggle();
		}
	});
}

function delFavo(modellId) {

	var page = $('#page').val();

	$.get('xmlhttp/favo.php', {action: 'del', modell: modellId, page: page}, function (data) {
		if (data) {
			if(data == 'reload'){
				document.location.reload(true);
			}else{
				var value = data.split("||");

				$('td.favoritter').html(value[1]);
				$('#menu_favo').html(value[0]);

				$('td.td_addToFavo, td.td_delFavo').toggle();
			}
		}
	});
}

/**** SLUT FAVORITTER ****/

/*** START TAF ***/

function tipAFriend(modellID){
	popup('/tipafriend.php?prodId='+modellID+'', 'Tips', 450, 650, 'no', 'no', 'no');
}

/*** SLUT TAF ***/



/**** Google - map ****/
function showGoogleMap(id, lat, long, text, zoom, control){

	$.getJSON('xmlhttp/map.php', { lat: lat, long: long, text: text, zoom: zoom }, function (data) {

		if (! data) {
			$('#'+id).hide();
			alert('Ingen karta funnen');
		} else if (! GBrowserIsCompatible()) {
			$('#'+id).hide();
			alert('Browser does not support googlemap.');
		} else {

			var map = new GMap2(document.getElementById(id));
			var pos = new GLatLng(lat, long);

			function addMarker (lat, lng, html) {
				var marker = new GMarker(new GLatLng(lat, lng));

				if(html.length > 2){
					GEvent.addListener(marker, "click", function() {
						marker.openInfoWindowHtml(html);
					});
				}

				GEvent.addListener(marker, "dragstart", function() {
					marker.closeInfoWindow();
				});

				map.addOverlay(marker);
			}

			if(control == 1){
				map.addControl(new GLargeMapControl());
			}
			map.setCenter(pos, parseInt(data.zoom));
			
			addMarker(lat, long, data.infoHtml);
		}
	});

}


/* Ticket */
function foundTicket(id){

	$.post('xmlhttp/ticket.php', { id: id }, function(data){
		/*
		data
		0 = Ingen modellID inskickat
		1 = Fel, denna biljett har ni redan valt
		2 = Första biljetten hittad
		3 = Andra biljetten hittad
		*/	
			
		data = data.split('|');

		if(data[0] == 1){
			alert(data[1]);
		}else if(data[0] == 2){
			alert(data[1]);
		}else if(data[0] == 3){
			location.href='/'+data[1];
		}else{
			alert('Something went wrong, try again');
		}

	});

	return false;
}
