$(document).ready( function() {

	//$(document).pngFix();

	//Sätt skuggan som 100%
	var height = $(document).height();
	$('#com_shadow').css('height', height+'px');

	$('#menu li').hover( function() {
		$(this).addClass('active');
		var id = $(this).attr('id');
		$('#menu li ul#m_'+id).show();
		$('#foldleft_'+id).show();
		$('#foldright_'+id).show();
	},function(){
		$(this).removeClass('active');
		var id = $(this).attr('id');
		$('#menu li ul#m_'+id).hide();
		$('#foldleft_'+id).hide();
		$('#foldright_'+id).hide();
	});


	/* HUSKELISTA */

	$('a.delhuske').click(function() {
		var modellId = $(this).attr('id');
		var html = $("#list").html();

		$("#list").ajaxStart(function() { $(this).html(html); });

		$.get('xmlhttp/huske.php', {action: 'del', modell: modellId}, function (data) {
			if (data) {
				document.location.reload(true);
			}
		});
	});

	$('a.flushhuske').click(function() {
		var html = $("#list").html();
		$("#list").ajaxStart(function() { $(this).html(html); });

		$.get('xmlhttp/huske.php', {action: 'flush'}, function (data) {
			document.location.reload(true);
		});
	});

	$('a.facebook').hover( function() {
		$('img.fb_off, img.fb_on').toggle();
	}, function() {
		$('img.fb_off, img.fb_on').toggle();
	});

	$('a.twitter').hover( function() {
		$('img.tw_off, img.tw_on').toggle();
	}, function() {
		$('img.tw_off, img.tw_on').toggle();
	});
});

function getProductPage(next) {

	var typer = $('#typer').val();
	var type = $('#type').val();
	var page = $('#page').val();
	var pages = $('#pages').val();
	var favoritter = $('#favoritter').val();
	var search = $('#search').val();
	var brand = $('#brand').val();
	var campaign = $('#campaign').val();
	var categoryID = $('#categoryID').val();

	if(next == 1){
		nr = parseInt(page) + 1;
	}else{
		nr = parseInt(page) - 1;
	}

	$('#page').attr('value', nr);

	$.get('xmlhttp/getproducts.php', { typer: typer, type: type, page: nr, pages: pages, favoritter: favoritter, search: search, brand: brand, campaign: campaign, categoryID: categoryID }, function( data ) {
		var value = data.split("||");
		$('#products .list').html(value[0]);
	});

	return false;
}


function selectShoe(modell_id){

	var favoritter = $('#favoritter').val();
	$('.list td').removeClass('active');
	$('.tds_'+modell_id).addClass('active');

	$.get('xmlhttp/getproduct.php', { modell_id: modell_id, favoritter: favoritter }, function( data ) {
		$('#products .product').html(data);
	});

	return false;
}



function showBigShoeImage(modellID) {

	$('#com_shadow').show();

	$.get('xmlhttp/getBigShoeImage.php', { modellID: modellID }, function( data ) {
		$('#product_bigimage').html(data);
		$('#product_bigimage').show();
	});
}

function closeBigShoeImage() {
	$('#product_bigimage').hide();
	$('#com_shadow').hide();
}

function changeBigImage(id, img) {
	$('#product_bigimage .active').removeClass('active');
	$('#product_bigimage .i_'+id).addClass('active');
	$('#product_bigimage .image').html('<img src="'+img+'">');
}




function show360Flash(modellID) {

	$('#com_shadow').show();
	$('body').addClass('hide');

	$.get('xmlhttp/get360Flash.php', { modellID: modellID }, function( data ) {
		$('#product_360').html(data);
		$('#product_360').show();
	});
}

function close360() {
	$('#product_360').hide();
	$('#com_shadow').hide();
	$('body').removeClass('hide');
}


function tipAFriend(modellID){
	popup('/tipafriend.php?prodId='+modellID+'', 'Tips', 450, 470, 'no', 'no', 'no');
}


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;
}


/* HUSKELISTA */

function addToHuske(modellId) {

	$.get('xmlhttp/huske.php', {action: 'add', modell: modellId}, function (data) {
		if (data) {
			alert(data);
		}
	});
}

/* 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]);
		}
	});
}

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]);
			}
		}
	});
}


/* BUTIKER */

function showMap(butikNo) {

	//$('#com_shadow').show();
	$('body').addClass('hide');

	$('#butikmap').hide();
	$('#butikmap-holder').find('a').empty();
	$('#butikmap-holder').show();
	$('#com_shadow').show();

	$.getJSON('xmlhttp/butikmap.php', {butikNO: butikNo}, function (data) {
		if (! data) {
			$('#butikmap').hide();
			alert('Ingen karta funnen');
		} else if (! GBrowserIsCompatible()) {
			$('#butikmap').hide();
			alert('Browser does not support googlemap.');
		} else {
			$('#butikmap').show();

			var map = new GMap2(document.getElementById("butikmap"));
			var pos = new GLatLng(data.mapLat, data.mapLon);

			function addMarker (lat, lng, html) {
				var marker = new GMarker(new GLatLng(lat, lng));

				GEvent.addListener(marker, "click", function() {
					marker.openInfoWindowHtml(html);
				});

				GEvent.addListener(marker, "dragstart", function() {
					marker.closeInfoWindow();
				});

				map.addOverlay(marker);
			}

			map.addControl(new GLargeMapControl());
			map.setCenter(pos, 7);

			addMarker(data.mapLat, data.mapLon, data.infoHtml);
			$('#butikmap-holder').prepend('<a id="closeMap" href="javascript:;"><img src="static/images/close_map.png" width="42" height="42" border="0" Onload="fixPNG(this);"></a>');
			$('#butikmap-holder').find('a').click(function() {
				$(this).empty();
				$('#map-overlay').hide();
				$('#butikmap').hide();
				$('#butikmap-holder').hide();
				$('img.karte').attr('src','static/images/map.jpg');
				$('#com_shadow').hide();
				$('body').removeClass('hide');
			});
		}
	});
}