$(document).ready( function() {

	$('input#doSend').click( function() {

		var passedValidation = true;
		var passedCode = true;
		
		$("#contactForm input").each(function() {
			if((this.type != "submit") && ($(this).val().length < 2)) { 
				passedValidation = false;
			} 
		});
			
		$("#contactForm textarea").each(function() {
			if(($(this).val().length < 2)) {
				passedValidation = false;
			}
		});

		if (!passedValidation) {
			alert("Alla fält är inte ifyllda.");
			return;
		}

		$('form#contactForm').submit();
		return;
	});

	$('#top .cart').hover( function() {
		var show = $('#showCart').val();
		if(show == 1){
			$('#top .cart .cart_items').show();
		}
	}, function(){
		$('#top .cart .cart_items').hide();
	});

	$('#top .prov').hover( function() {
		$('#top .prov .cart_items').show();
	}, function(){
		$('#top .prov .cart_items').hide();
	});


	$('.largeFabric').click( function(){
		var img = $(this).attr('name');
	
		$.get('templates/popup/largeFabric.tpl.php', { img: img }, function(data){
			showPopup(data);
		});
	});

	fixButtons();

	$('#contactPage form input[name=company]').attr('value', 'dittsolskydd');
});

/* Lägger in data i div-popup */
function showPopup(data){
	$('#shadow .border .body .space').html(data);
	fixButtons();

	$('#shadow').show();

	//Beräkna bild
	var height = $('#shadow .border').height();

	var window_height = $(window).height();

	var top = (window_height - height) / 2;

	$('#shadow .border').css('top', top+'px');
}

/* Stäng div-popup */
function closePopup(){
	$('input[name=rowID]').attr('value', '0');
	$('#shadow').hide();
}


function fixButtons(){
	$('a.button').each( function() {
		var cl = $(this).attr('class');

		if(!$(this).parent().hasClass('middle')){
			$(this).wrap('<div class="'+cl+'"><div class="middle"></div></div>');
			$(this).parent().before('<div class="left"></div>');
			$(this).parent().after('<div class="right"></div>');
		}
	});
}

//changes main image on product page
function changeProductMainPicture( file, imageID, productID ) {
	$('#productImageLargeTD').html('<img src="'+file+'">');

	price = $('#prod_price').val();

	$('option.'+imageID).attr('selected', 'selected');

	$.get('xmlhttp/checkPrice.php',{ productID: productID, imageID: imageID, price: price }, function (data) {
		$('.price-body').html(data+':-');
	});
}



function popup (url, name, width, height, status, menu, resizable, scrollbars) {
	var prop = 'toolbar=no,location=no,directories=no,copyhistory=no,';
	prop += 'status='+ (status ? 'yes' : 'no') +',';
	prop += 'resizable='+ (resizable ? resizable : 'yes') +',';
	prop += 'menu='+ (menu ? 'yes' : 'no') +',';
	prop += 'scrollbars='+ (scrollbars ? scrollbars : 'yes') +',';
	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;
}


function proceedToPayment() {

	//alert($('form#payForm input#confirmDeal').attr('value'));
	// Insert code here.
	listenForPayment(); 
	custompop('aurigaforward.php', 640, 480, 'yes', 'no')
	
}

function trigPayment() {
	document.forms['orderForm'].submit();
}


function listenForPayment() {
	
	$.get('xmlhttp/aurigacheck.php', null, function (data) {
		if (data == 'OK') {
			alert("Betalningen är godkänd, klicka OK för att slutföra beställning.\n\nPayment accepted, please click OK to complete your order.");
			document.forms['orderForm'].submit();
		}
		else {
			setTimeout ( 'listenForPayment()', 5000 );
		}
	});
}


function custompop(url, width, height, status, menu) {
   win = window.open(url, 'profile_popup', 'toolbar=no,location=no,directories=no,status=' + status +  ',menubar=' + menu + ',scrollbars=yes,resizable=yes,copyhistory=no,width=' + width + ',height=' + height);
   win.focus();
}

Cufon.replace ('#menu li a, #top .montera .bubble .body .middle, div#list-fabrics .free_sample .body', { fontFamily: 'Barmeno', hover: true });


//Lägg till vävprov
function addProv(fabricID, type){

	$.get('xmlhttp/addProv.php', { fabricID: fabricID, type: type }, function(data){

		var list = data.split('|');

		if(list[0] == 1){
			$('.prov').html(list[2]);
			$('li#prov').hide();
		}

		showPopup(list[1]);

		reload();

		$('#shadow').show();
	});

	return false;
}

//Ta bort vävprov
function deleteProv(id, type, reload){

	$.get('xmlhttp/deleteProv.php', { id: id, type: type }, function(data) {

		//alert(data);
		if(reload == 1){
			parent.location.reload();
		}else if(data == 'tom'){
			$('#top .prov').hide();
		}else{
			if(data == type){
				$('#top .prov .cart_items tr.'+type).fadeOut();
			}else{
				$('#top .prov .cart_items tr#p_'+id).fadeOut();
			}
		}
	});
}


//Välj modell
function selectModel(type, fabricID){

	$.get('xmlhttp/selectModel.php', { type: type, fabricID: fabricID }, function(data){
		showPopup(data)
	});

}
