$('document').ready( function () {
	/* INIT */

	if ($('#subname')[0]) {
		$('#subname')[0].focus();
	} else if ($('#subpass')[0]) {
		$('#subpass')[0].focus();
	}

	$(window).scroll(function () {
		if ($('#confirm-alert-centered').css('display') == 'block') {
			$('#confirm-alert-centered').vCenter();
		}
	});


	// Hide submenu-items
	$('a.leftsubmenu-off').hide();
	$('a.leftsubsubmenu-off').hide();

	// Hide confirm-overlay div...
	$('div.confirm-overlay').hide();
	$('div.confirm-alert').hide();

	// Load Cart
	$('#cart').load('xmlhttp/cart.php');

	/* INIT */

	// Menu Toggle Functions
	$('a.leftmenu-off, a.leftmenu-on').click( function () {
		var itemname = 'item'+ $(this).attr('name');

		if ($(this).attr('class') == 'leftmenu-on') {
			$(this).removeClass('leftmenu-on');
			$(this).addClass('leftmenu-off');
		} else {
			$(this).removeClass('leftmenu-off');
			$(this).addClass('leftmenu-on');
		}

		$('a.leftsubmenu-off, a.leftsubmenu-on').each( function (i) {
			if ($(this).attr('name') == itemname) {
				$(this).toggle();
			}
		});
	});


	// Change preview of profile clothes
	$('#styleSetBold').click(function() {
		$('#profileForm').removeClass('useItalic');
	});
	
	$('#styleSetBoldItalic').click(function() {
		$('#profileForm').addClass('useItalic');
	});


	//Update profile data onchange
	$('div#profileForm .radio, form#profileForm .radio').bind($.browser.msie ? "click" : "change",function() {
		updateProfileForm();
	});

	//Update profile data for IE6/IE7
	$('div#profileForm .text, form#profileForm .text').bind($.browser.msie ? "blur" : "change",function() {
		updateProfileForm();
	});

	function updateProfileForm() {
		$.get('xmlhttp/set_profilestyle.php', { profileStyle : $('input[name=profileStyle][checked]').val(), profileShape : $('input[name=profileShape][checked]').val(), profileFarm : $('#profileFarm').val(), profileName : $('#profileName').val() });
	}

	// Buy-Button On Product Page (with formdata)
	$('input[@type=button].button-buy').click( function () {
		var fdata = $('#productForm input, #productForm select').serialize();
		var error = '';

		if($('.hide_color').val() == ''){
			error += "color";
		}

		if($('.hide_size').val() == ''){
			if(error != ''){
				error += '|';
			}
			error += "size";
		}

		if(error != ''){
			$.get('xmlhttp/set_buyerror.php', { error: error }, function (data) {
				$('.buy-errorcontent').html(data);
				$('div.confirm-overlay').show();
				$('div.buy-error').show();
			});
			return false;
		}

		$('div.confirm-overlay').show();

		/*if($('#sel_profile').val() != 0){
			$.get('xmlhttp/set_profile.php', { id: $('#sel_profile').val() }, function () {});
		}

		if($('#profile').val() == 1){
	
			$.get('xmlhttp/checkprofile.php', function (data) {

				if(data == 0){
					$('div.profile-select').show();
				}else{	
					$('div.profile-select').hide();
					$('div.confirm-alert').show();
					buyProduct(fdata);
				}
			});
		}else{*/
			$('div.confirm-alert').show();
			buyProduct(fdata);
		//}
	});

	$('a.confirm-overlay').click(function () {
		$('div.confirm-overlay').hide();
		$('div.confirm-alert').hide();
	});

	$('a.buy-errorclose').click(function () {
		$('div.confirm-overlay').hide();
		$('.buy-error').hide();
	});

	


	/* Subshop login */
	$('#subpass_send').click(function () {
		var error = false;
		var user = $('#subname').attr('value');
		var pass = $('#subpass').attr('value');

		if (! pass) {
			alert('Felaktigt login');
		} else {
			$.post('xmlhttp/subshop.php', { usr: user, pwd: pass }, function (data) {
				if (data == 1) {
					$('#subshoplogin').hide();
					$('#subshoplogin_form').hide();
					document.location.reload(true);
					return true;
				} else {
					alert('Felaktigt login');
					return false;
				}
			});
		}
		return false;
	});

	$('.select_shopID').click( function() {
		shopID = parseInt($(this).attr('id'));

		if(shopID){
			$.post('xmlhttp/selectShop.php', { shopID: shopID }, function (data) {

				if (data == 'error') {
					alert('Felaktigt login');
				} else {
					top.location.href = data;
				}
			});
		}
	});


	/* SHOW BRANDFILTER */
	$('#showbrands').click(function() {
		$('#brandfilter').toggle();
	});

	$('.showprod').click(function() {
		id = $(this).attr('id');

		$('#product_'+id).toggle();
	});

	$('input.amount').keyup(function() {
		$('#cart').html('<div style="padding: 10px 0px 0px 40px; color: red;">Uppdaterar...</div>');

		var amount = $(this).val();
		var myid = $(this).attr('id');
		var uid = false;
		var pro = false;

		if (amount == 0 && amount != ''){
			alert("Fel antal, vill ni ta bort produkter tryck på Ta bort!");
		}

		tmp = myid.split('_');
		uid = tmp[0] ? tmp[0] : tmp;
		pro = tmp[1] ? tmp[1] : false;

		$.post('xmlhttp/changeamount.php', { row: uid, profile: pro, amount: amount }, function (data) {
			if (data != 'error') {
				list = data.split('||');
				$('input#'+ myid).attr('value', list[2]);
				$('#tot').html(list[0]);
				$('#tot_amount').html(list[1]);
			}
			$('#cart').load('xmlhttp/cart.php');
		});

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

// Buy product
function buyProduct(fdata) {
	$.post('xmlhttp/cartaction.php', fdata, function (data) {
		if (data != '' && ! isNaN(data)) {
			$.get('xmlhttp/cart.php', { sel: data }, function (html) {
				$('#cart').html(html);
				$('#cart').find('select').blink('#bc8f2d', 'none', 150, 4);
			});
		} else {
			if (data) {
				// No alert on production site...
				// alert(data);
			}
			$('#cart').load('xmlhttp/cart.php');
		}
	});
}

// Set Product ColorCodes
function setColor (sectionId, propertyId, colorName) {
	$('div.colorcode-on').each( function (i) {
		$(this).removeClass('colorcode-on');
		$(this).addClass('colorcode');
	});

	var cId = '#'+ propertyId; // ClickedId
	var sId = '#prodSection_'+ sectionId; // sectionId
	var dId = 'div#prodColor_'+ sectionId; //divId

	$(dId +' span').html(colorName);
	$(sId).attr('value', propertyId);
	$(cId).addClass('colorcode-on');
}

//Show and hide function
function visa(id) { 
  /*if(document.getElementById) {
  	var obj = document.getElementById(id);
  } 
  else if(document.all) { 
  	var obj = document.all(id);
  } 
  
  if (obj == null || obj == false)
  	return;
  
  if(obj.style.display == 'none'){ obj.style.display = ''; } 
  else { obj.style.display = 'none'; } */

  $('#'+id).toggle();

  if($('#'+id).css('display') == 'block'){
		$('.'+id).attr('class', id+' childs active');
  }else{
		$('.'+id).attr('class', id+' childs');
  }
} 

/** 
 * NON JQUERY METHODS 
 *
 */
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();
}


function trigPayment() {
	alert('check for valid payment');
}

function listenForPayment() {
	
	$.get('xmlhttp/dibscheck.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 );
		}
	});
	
	
}



//changes main image on product page
function changeProductMainPicture( filename, imageID, pos, totalthumbs, productID, altText, bigPic ) {
	
	var ih = '<a href="javascript:custompop(\'productimage.php?imageID=' + imageID + '\', 400, 400, \'no\', \'no\')" style="text-decoration: none; color: #bbb;"><img alt="'+altText+'" align="center" src="' + filename + '" border="0" alt="" id="productImageLarge" class="productimage"><br>'+bigPic+'...</a>';
	var largeImageTD = document.getElementById('productImageLargeTD');
	largeImageTD.innerHTML = ih;

	$('#product_pdf').attr('href', 'productpdf.php?productID='+productID+'&imageID='+imageID);

	resetThumbnails( totalthumbs );
	activateThumbnail( pos, true );
}

//resets class names for thumbnails on product page
function resetThumbnails( totalthumbs ) {
	for (i=0; i<totalthumbs; i++) {
		activateThumbnail( i, false );
	}
}

//sets class name for a specific thumbnail on product page
function activateThumbnail( pos, mode ) {
	
	var thumbnail = document.getElementById('thumbnailBox' + pos);
	var imgThumbnail = document.getElementById('thumbnailImage' + pos);
	
	
	if (mode) {
		thumbnail.className='productImageThumbnail-selected';
		imgThumbnail.className='productImageThumbnail-selected';
	}
	else {
		thumbnail.className='productImageThumbnail';
		imgThumbnail.className='productImageThumbnail';
	}
}

function pause(ms) {
	var now = new Date();
	var exitTime = now.getTime() + ms;
	while (true) {
		now = new Date();
		if (now.getTime() > exitTime)
			return;
	}
}

/* CURRENCY SELECTOR */
function showCurrencyChoices( availableOptions ) {
	
	//put together options ans insert them in the div
	var options = availableOptions.split('-');
	var x=0;
	var parts = null;
	var html = '';
	for (x=0; x<options.length; x++)
	{
		parts = options[x].split(';');
		html += '<a href="currencyhandler.php?action=selectCurrency&currencyID=' + parts[0] + '">' + parts[1] + '</a><br/>';
	} 
	
	$('#currencySelectorOptions').html(html);
	$('#currencySelectorOptions').toggle();
}


/*
function deleteUserDialog( userID, name ) {
	if (confirm('Vill du verkligen ta bort ' + name + '?')) {
		window.location = '_personaldelete.php?userID=' + userID;
	}
}
*/
