$(document).ready( function () {
	/* INIT */
	var height = $(document).height();

	if(height < $(window).height()){
		height = $(window).height();
	}

	$('.shadow').css('height', height+'px');
	
	/* 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);
			}
		});
	});

	$('input.flushhuske').click(function() {
		$.get('xmlhttp/huske.php', {action: 'flush'}, function (data) {
			document.location.reload(true);
		});
	});

	$('.start_map area').hover( function(){
		var cl = $(this).attr('class');
		var name = $(this).attr('name');

		$('.start_map .pointer span').html(name);
		$('.start_map .pointer').show();

		var id = $(this).attr('id').split(',');

		$('.start_map .pointer').animate({
			left: (id[0]-15), top: (id[1])
		}, 500);

		$('.start_map img.'+cl).css('z-index', 10);

		$('.start_map img.'+cl).animate({
			opacity: 'show'
		}, 500, function(){
			$('.start_map img.'+cl).css('z-index', 2);
		});
	}, function(){
		var cl = $(this).attr('class');
		$('.start_map .pointer').hide();
		$('.start_map img.'+cl).animate({
			opacity: 'hide'
		}, 500, function(){
			$('.start_map img.'+cl).css('z-index', 0);
		});		
	});

	//Kolla Kund-ekstra formulär
	$('form#melding').submit( function(){

		var email = $('input#email').val();
		var name = $('input#navn').val();
		var cellphone = $('input#cellphone').val();

		var error = '';

		at=email.indexOf("@");
		lastat=email.lastIndexOf("@");
		dot=email.lastIndexOf(".");
		
		if (email.length > 255){
			error += "Bruk maks 255 tegn \"E-postadresse\" felt.\n";
			$('input#email').focus();
		}else if (email == ""){
			error += "Sett inn verdi for \"E-postadresse\" felt.\n";
			$('input#email').focus();
		}else if (email.length < 7) {
			error += "Bruk minst 7 tegn i \"E-postadresse\" felt.\n";
			$('input#email').focus();
		}else if(at<1||at!=lastat||dot<at){
			error += "Har du oppgitt feil e-postadresse? Prøv igen.\n";
		}

		if (name.length < 2) {
			if(error.length == 0){
				$('input#navn').focus();
			}
			error += "Bruk minst 2 tegn i \"Navn\" felt.\n";
		}

		/*if (cellphone.length < 7) {
			if(error.length == 0){
				$('input#cellphone').focus();
			}
			error += "Bruk minst 7 tegn i \"Mobiltelefon\" felt.\n";
		}*/

		if(error.length == 0){
			return true;
		}else{
			alert(error);
			return false;
		}

	});
	
	//
	// Competition
	//
	$('#top10').click( function() {
		$('#top10').animate({opacity:'hide'}, 'fast');
		$('div.competition').addClass('top10');
		$('#change_box').animate({opacity:'0'}, 'fast', function() {
			$.ajax({
				type: "POST",
				url: "xmlhttp/ajax_template.php",
				data: "tpl=competition/last_opp_din_sko_top10",
				success: function(data){
					$('#change_box').html(data);
					$('#change_box').animate({opacity: '1'}, 'fast');
					$('#top10-back').animate({opacity: 'show'}, 'fast');
				}
			});
		});
	});
    
	$('#top10-back').click( function() {
		$('div.competition').removeClass('top10');
		$('#top10-back').animate({opacity:'hide'}, 'fast');
		$('#change_box').animate({opacity:'0'}, 'fast', function() {
			$.ajax({
				type: "POST",
				url: "xmlhttp/ajax_template.php",
				data: "tpl=competition/last_opp_din_sko_competition",
				success: function(data){
					$('#change_box').html(data);
					$('#change_box').animate({opacity: '1'}, 'fast');
					$('#top10').animate({opacity: 'show'}, 'fast');
					reloadClick();
				}
			});
		});
	});
	
	$('#pan-arrow-left').attr('src', 'static/images/competition/nav-left-fade.jpg');
	
	$('#upload-form-submit').submit( function() {
		var error = false;
		var header = $('#header-field').val();
		var name = $('#name-field').val();
		var email = $('#email-field').val();
		var phone = $('#phone-field').val();
		var message = $('#message-field').val();
		var file = $('#file-field').val();
		
		at=email.indexOf("@");
		lastat=email.lastIndexOf("@");
		dot=email.lastIndexOf(".");
		
		if (email.length > 255 || email.length < 7 || email == ""){
			error = true;
		}
		
		if(at<1||at!=lastat||dot<at){
			error = true;
		}

		if (name.length < 2) {
			error = true;
		}
		
		if(phone == '' || file == '') {
			error = true;
		}
		
		/*if(message == '' || message.length > 500) {
			error = true;
		}*/

		if(error) {
		    $('#form-error').show();
		    return false;
		} else {
		    return true;
		}
	});
	
	$('#julkalender').submit( function(){
		var form = $(this).formSerialize();

		$('div.julekalender .error').hide();

		$.post('xmlhttp/save_christmas.php', form, function(data){

			if(data == 'ERROR'){
				$('div.julekalender .error').show();
			}else if(data == 'ALREADY'){
				$('div.julekalender .already').show();
			}else{
				$('div.julekalender .competition_sent').show();
				$('div.julekalender .user_information').hide();
			}
		});

		return false;
	});

	reloadClick();
}); 

//Globals
    var panIsClickable = true;
    var loadingContribution = false;
    var active;
    
Cufon.replace ('h1, .menu li a, .shoe .price div, #startpage .box .price, div#bottom .bar a, #bottom h3, #shoe .price div, h2', { fontFamily: 'Helvetica Neue LT Std' });

//Competition
Cufon.replace ('#competition-popupHeader, .competition-popup .votes, .upload-form, div.competition-thanks .middle .bold_span, .upper .b, .middle a, #shoe_brand, #shoe_price_and_size', { fontFamily: 'Helvetica Neue LT Std' });

function nemo(namn,foretag){
	location.href= 'mailto:'+namn+'@'+foretag;
	return false;
}

/*
Visa sko
*/
function showShoe(shoeID){

	$.get('xmlhttp/getShoe.php', { shoeID: shoeID }, function(data){
		$('.popup').html(data);

		var top = $(window).scrollTop();
		
		var height = ($(window).height() - $('.popup').height()) / 2 + top;

		if(height < 0){
			height = 0;
		}

		$('.popup').css('top', height+'px');

		Cufon.refresh();

		$('.shadow, .popup').show();
	});

	return false;
}

function closeShoe(){
	$('.popup').html('');
	$('.shadow, .popup').hide();
}


/* HUSKELISTA */
function addToHuske(modellId) {
	$.get('xmlhttp/huske.php', {action: 'add', modell: modellId}, function (data) {
		if (data) {
			alert(data);
		}
	});
}


function pagination(direction, page, slide){

	$('a.pop_left, a.pop_right').hide();
	$('div.dis_pop_left, div.dis_pop_right').show();

	var active_page = parseInt($('input#active_page').val());

	//Kolla om next
	if(direction == 'right'){
		var show_page = active_page + 1;
		//move to the other side		
		$('div.block_'+show_page).show();
	}else if(direction == 'left'){
		var show_page = active_page - 1;
	}else{	
		show_page = page;
		direction = 'left';
	}

	$('div.block_'+active_page).hide();
	$('div.block_'+show_page).show();

	showPagination(show_page);
	$('input#active_page').attr('value', show_page);
}

function showPagination(page){
	// Animation complete, check if hide button.

	if(page > 1){
		$('a.pop_left').show();
		$('div.dis_pop_left').hide();
	}

	var max = parseInt($('input#active_pages').val());

	if(page < max){
		$('a.pop_right').show();
		$('div.dis_pop_right').hide();
	}

	$('#shoes .pagination td.active').removeClass('active');
	$('#shoes .pagination td.p_'+page).addClass('active');

	window.location.hash = page;

	$('input#active_page').attr('value', page);
}

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;
}

//Competition scroll

function panLeft() {
    
    if(panIsClickable) {
	panIsClickable = false;
	var pages = parseInt($('#pages').val());
	var active = parseInt($('#active_page').val());
	var prev = (active - 1) >= 1 ? (active - 1) : pages;
	var next = (active + 1) <= pages ? (active + 1) : -1;
	var fade = (active - 2) >= 1 ? false : true;

	var width = $('.holder').width();
    
	if(active == pages) {
	    $('#pan-arrow-right').attr('src', 'static/images/competition/nav-right.jpg');
	}

	changeShoe();
	
	if(prev != pages) {
	$('.scroller').animate({
	    left: '+='+width},
	    'slow', 'linear', function() {
		$('#active_page').attr('value', prev);
		panIsClickable = true;
		if(fade) {
		    $('#pan-arrow-left').attr('src', 'static/images/competition/nav-left-fade.jpg');
		}
	    });
	} else {
	    panIsClickable = true;
	}
    }
}
function panRight() {
    if(panIsClickable) {
	panIsClickable = false;
	var pages = parseInt($('#pages').val());
	var active = parseInt($('#active_page').val());
	var prev = (active - 1) >= 1 ? (active - 1) : pages;
	var next = (active + 1) <= pages ? (active + 1) : -1;
	var fade = (active + 2) <= pages ? false : true;

	var width = $('.holder').width();

	changeShoe();
	
	if(active == 1) {
	    $('#pan-arrow-left').attr('src', 'static/images/competition/nav-left.jpg');
	}
	
	if(next != -1) {
	$('.scroller').animate({
	    left: '-='+width},
	    'slow', 'linear', function() {
		$('#active_page').attr('value', next);
		panIsClickable = true;
		if(fade) {
		    $('#pan-arrow-right').attr('src', 'static/images/competition/nav-right-fade.jpg');
		}
	    });
	} else {
	    panIsClickable = true;
	}
    }
}

//Contributions scroll
function changeContribution(action) {
    
    if(!loadingContribution) {
		loadingContribution = true;
		var prev = (active - 1) >= 1 ? (active - 1) : (numEntry -1);
		var next = (active + 1) <= (numEntry - 1)? (active + 1) : 0;
		
		var id = eval(action);
		
		$('#competition-popupImage').animate({ 'opacity': '0'}, 'slow');
		$('#competition-popupHeader').animate({ 'opacity': '0'}, 'slow');
		$('#competition-popupStemmer').animate({ 'opacity': '0'}, 'slow');
		$('#competition-popupText').animate({ 'opacity': '0'}, 'slow', function() {
			$('#competition-popupImage').attr('src', 'static/images/competition/' + entryData[id]['image']);
			$('#competition-popupHeader').html(entryData[id]['header']);
			$('#competition-popupText').html(entryData[id]['text']);
			$('#competition-popupStemmer').html(entryData[id]['likes']);
			$('#competition-popupImage').animate({ 'opacity': '1'}, 'slow');
			$('#competition-popupHeader').animate({ 'opacity': '1'}, 'slow');
			$('#competition-popupStemmer').animate({ 'opacity': '1'}, 'slow');
			$('#competition-popupText').animate({ 'opacity': '1'}, 'slow', function() {
			loadingContribution = false;
			active = id;
			});
		});
    }
}

function showMessage(msg) {
    if($('#message-popup').is(':hidden')){
	var scroll = $(window).scrollTop() + ($(window).height() / 2);
	$('#message-popup').css('top', scroll+'px');
	$('#message-popup-span').html(msg);
	$('#message-popup').fadeIn('slow').delay(2000).fadeOut('slow');
    }
}

function showContribution(id) {

	var scroll = $(window).scrollTop() + 100;

	$('.shadow').show();
	$('.competition-popup').css('top', scroll+'px');
    $('.competition-popup').show();
    

	changeShoe();

	$.get('xmlhttp/comp_shoe.php', { id: id }, function(data){
		$('.competition-popup').html(data);
		reloadClick();

		window.location.hash = id;
	});

    active = id;
}

function hideDiv(name) {
    $(name).css('display', 'none');
}



function likeEntry(id, reload){
	$.ajax({
		type: "POST",
		url: "xmlhttp/like_entry.php",
		data: "id="+id,
		success: function(data){
			if(data != 'error' && data != 'IP_ALREADY_VOTED') {

				if(reload == 1){
					showContribution(id);
				}
				
				$('#like_amount_for_'+id).html(data);
			}
			
			if(data == 'IP_ALREADY_VOTED') {
				showMessage('Du har allerede stemt på dette bidraget');
			}
		}
	  }); 
}

function changeShoe() {

    $.ajax({
	type: "POST",
	url: "xmlhttp/comp_get_shoe.php",
	data: "from=js",
	success: function(data){

	    $('#shoe_box').animate({'opacity':'0'}, 'fast', function() {
		    $('#shoe_box').html(data);
		}).animate({'opacity':'1'}, 'fast');
	    
	}
    });  
}


function getKeyForID(id){
    for(var i in entryData){
	if(entryData[i]['id'] == id){
	    var key = i;
	}
	
    }
    
    if(key){
	return key;
    } else {
	return false;
    }
    
}

function reloadClick() {

	$('#thanks-close').click( function() {
		$('.shadow').hide();
		$('.competition-thanks').hide();	
	});
	
	$('#thanks-return').click( function() {
		$('.shadow').hide();
		$('.competition-thanks').hide();	
	});

	$('#crop-close').click( function() {
		$('.shadow').hide();
		$('.competition-crop').hide();	
	});


	//Start
	$('#start-pan-left').click( panLeft );
    
	$('#start-pan-right').click( panRight );
    
	$('#start-upload-button').click( function() {
		$('.shadow').show();
		$('.competition-upload').show();
		$(window).scrollTop(0);

	});
	
	//Contribution
	$('#contribution-close').click( function() {
		$('.shadow').hide();
		$('.competition-popup').hide();
	});
	
	$('#contribution-nav-left').click( function() {
		changeContribution('prev');
	});
	
	$('#contribution-nav-right').click( function() {
		changeContribution('next');
	});
	
	$('#contribution-like-current').click( function() {
		likeEntry(false);
	});
	
	//Upload
	$('#upload-close').click( function() {
		$('.shadow').hide();
		$('.competition-upload').hide();
		$('#form-error').hide();
	});

	Cufon.refresh();
}


