$(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();
	});

	//Registrera Eurosko Exclusive
	$('form#exclusive_reg_form').submit( function(){

		var form = $('form#exclusive_reg_form').formSerialize();

		$('img.error').hide();

		$.getJSON('handlers/exclusive_register.handler.php', form, function(data){
			if(data.status != 2 && data.status != 0){
				$('.register_exclusive').hide();
				$('.register_exclusive_thanks').show();
			}else if(data.status == 2){
				$('.register_exclusive').hide();
				$('.register_exclusive_already').show();
			}else{
				
				var fields = data.fields.split('|');

				for(var f in fields){
					$('img.e_'+fields[f]).show();
				}
			}
		});

		return false;
	});

	//Logga in Eurosko Exclusive
	$('form#login_exclusive').submit( function() {

		var form = $('form#login_exclusive').formSerialize();

		$.getJSON('handlers/exclusive_login.handler.php', form, function(data){

			if(data.status != 0){
				parent.location='/kategori/'+data.status+'/exclusive.html';
				//alert(data.text);
			}else{
				alert('FEL');
			}
			
		});

		return false;
	});

	//Skicka TAF Eurosko Exclusive
	$('form#exclusive_taf_form').submit( function(){

		var form = $('form#exclusive_taf_form').formSerialize();

		$('img.error').hide();

		$.get('handlers/exclusive_taf.handler.php', form, function(data){

			if(data == -1){
				$('img.error').show();
			}else if(data == 1){
				$('#exclusive_taf_form div.sent').show();
			}else{
				alert('Could not send mail');
			}
		});

		return false;
	});

	//Skicka Rihanna contest
	$('form#rihannaform').submit( function(){

		var form = $('form#rihannaform').formSerialize();

		$.get('handlers/melissa.handler.php', form, function(data){

			if(data > 0){
				$('form#rihannaform').hide();
				$('.confirm').show();
			}else if(data == -1){
				$('.error').html('Du har skrivit in fel skomodell');
				$('.error').show();
			}else{
				$('.error').html('Alla *-fält måste fyllas i');
				$('.error').show();
			}
		});

		return false;
	});
});

function rihannaTicket(id){

	$.post('xmlhttp/rihannaTicket.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
		*/		

		if(data == 1){
			alert('Du har redan denna biljett');
		}else if(data == 2){
			alert('Bra jobbat, bara en biljett kvar!');
		}else if(data == 3){
			location.href='/melissahorn.php';
		}else{
			alert('Something went wrong, try again');
		}

	});

	return false;
	

}


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, 650, '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]);

			$('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();
			}
		}
	});
}


/* 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');
			});
		}
	});
}

//Avanmälan nyhetsbrev
function removeSubscription(){

	$.post('handlers/exclusive_unregister.handler.php', function(data){
		if(data == 1){
			$('.utmelding_status').show();
		}else{
			alert("Something went wrong, try again!");
		}
	});

}
