var galleryType;
var imageID;
var catImage = new Array();

function initProductCategories () 
{
	if ( $('div#category_overview_item').length == 1 ) 
	{
		$('div#category_overview_item').find("li a").each(function() 
		{
			var item = $(this);
			$(this).hover(function () 
			{
				$.post("/", { action: "fetchThumb", productID: $(this).attr('product')}, function (response) 
				{
					response = eval('('+response+')');
					
					if (response.image) 
					{
						if ($(item).parents(".item_content").find("img.thumb").length == 1) 
						{
							$(item).parents(".item_content").find("img.thumb").attr("src", response.image);
						}
						else 
						{
							$(item).parents(".item_content").find("img.category_thumb").css('display', 'none');
							$(item).parents(".item_content").prepend("<img class=\"thumb\" src=\"" + response.image + "\" />");
						}
					}
				});
			}, function ()
			{
				$(item).parents(".item_content").find("img.category_thumb").css('display', 'block');
				$(item).parents(".item_content").find('img.thumb').remove();
			});
			
		});
	}
}

$(document).ready(function () 
{

	// initProductCategories();

	var imageID;
	
	// BEGIN: SELECT BOX

	$('.selectBox').css('display', 'none');
	$('.selectBoxBottom').css('display', 'none');

	$('.selectButton').click(
		function () {
			if ( $(this).next('.selectBox').css('display') == 'none' )
			{
				$(this).next('.selectBox').css('display', 'block');
				$('.selectBoxBottom').css('display', 'block');
			}
			else
			{
				$(this).next('.selectBox').css('display', 'none');
				$('.selectBoxBottom').css('display', 'none');
			}
		}
	);

	$('.selectBox>ul>li').click(
		function () 
		{
			if ($('.selectedOption').text () != $(this).children('.option').text())
			{
				$('.selectedOption').text ( $(this).children('.option').text() );

				var language_code = $(this).children('.value').text();
				parts = location.hostname.split ('.');

				if (parts.length >= 3)
				{
					parts[0] = language_code;
				}
				else
				{
					a = new Array (language_code);
					parts = a.concat(parts);
				}
				new_host = 'http://' + parts.join(".") + location.pathname;

				location.href = new_host;
			}
			$('.selectBox').css('display', 'none');
			$('.selectBoxBottom').css('display', 'none');
		}
	);

	$(document).bind('click', function(e) {
		var $clicked = $(e.target);
		if (! $clicked.parents().hasClass("languageSelect"))
		{
			$('.selectBox').css('display', 'none');
			$('.selectBoxBottom').css('display', 'none');
		}
	});

	// END: SELECT BOX

	$('#loginButton').click(
		function () {
			$('#loginContainer').css('display', 'block');
		}
	);
	
	$('a[rel=gallery_group]').fancybox({
		'opacity'		: true,
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic',
		'titlePosition'	: 'inside',
		'titleFormat'	: formatTitle,
		'onComplete': function () {

		  if ( $("#fancybox-content").find("object").length > 0 ) {

		    $("#send_picture").hide();

		  } else {

		    $("#send_picture").show();

		  }
		  
		  $.fancybox.resize();

		}
	});


	$('a[rel=product_group]').fancybox({
		'opacity'		: true,
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic'
	});

	$('a[rel=profile_group]').fancybox({
		'opacity'		: true,
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic'
	});

	if ($('#search').val() == '')
	{
		$('#search').val('search...');
	}

	$('#search').focus(function () {
		if ($(this).val() == 'search...')
		$(this).val('');
	});

	$('#search').blur(function () {
		if ($(this).val() == '')
			$(this).val('search...');
	});

	$('[name=txtUsername]').focus(function () {
    if ($(this).val() == 'username')
    $(this).val('');
  });

  $('[name=txtUsername]').blur(function () {
    if ($(this).val() == '')
      $(this).val('username');
  });

	$('[name=txtPassword]').focus(function () {
    if ($(this).val() == 'password')
    $(this).val('');
  });

  $('[name=txtPassword]').blur(function () {
    if ($(this).val() == '')
      $(this).val('password');
  });


	$(document).bind('click', function(e) {
		var $clicked = $(e.target);
		if (! $clicked.parents().hasClass("loginContainer") && ! $clicked.hasClass("loginContainer"))
		{
			$('#loginContainer').css('display', 'none');
		}
	});

	$('#slider').slides({
		preload: true,
		play: 5000,
		pause: 2500,
		hoverPause: true
	});

	$('.profile-department').each (function ()
	{
		$(this).attr ('href', 'javascript:void(0);');
		$(this).click (
			function ()	{
				if ($(this).next().css ('display') == 'none')
					$(this).next().css ('display', 'block');
				else
					$(this).next().css ('display', 'none');
			}
		);
	});

	$('.faq-question').click (function ()
	{
		if ($(this).next().next().css ('display') == 'none')
		{
			$(this).next().next().css ('display', 'block');
			question_id = $(this).next().text ();
			$.post ('/support/faq/add/click/register/' + question_id);
		}
		else
			$(this).next().next().css ('display', 'none');
	});

	$('.email').each (function ()
	{
		address = $(this).text ();
		new_address = address.replace("[at]", "@");
		$(this).text (new_address);
	});
	
	
	slides = $('.slides_control').children();
	// no slides? remove the slides div
	if (slides.length == 0)
	{
		$('#slider').remove();
	}
	// if there is only 1 slide, no navigation needed
	if (slides.length == 1)
	{
		$('.navigation').css ('display', 'none');
	}

	/* WEBSHOP */
	
	$('#shopping-cart>a').click (function () {
		$('.cart-items').slideToggle ('fast');
	});
	
	$('.cart-remove-item').click (function ()
	{
		$('#cart-action').val('remove');
		$('#cart-article_id').val($(this).attr('article'));
		$('#cart-form').submit ();
	});
	
	$('.update-cart').click (function ()
	{
		$('#cart-action').val('quantity');
		$('#cart-form').submit ();
	});
	
	$('.checkout').click (function ()
	{
		$('#cart-action').val('checkout');
		$('#cart-form').submit ();
	});
	
	
	$('#ogone_submit').click (function ()
	{
		reference = $('#reference').val();
		if (reference)
		{
			$.post('/shopping-cart/add/reference', {action: 'reference', reference: reference}, function (response){});
		}
		$('#order-form').submit();
	});
	
	$('input[name=delivery]').click (function ()
	{
		type = $(this).val();
		$.post('/shopping-cart', {action: 'delivery', orderdelivery: type}, function (response){
			location.reload(true);
		});
	});
	
	$('.add-to-cart').click (function ()
	{
		value = $(this).prev('.cart-count').val();
		
		if (value > 0)
		{
			$(this).attr ('href', $(this).attr ('href') + '/' + value);
		}
		else
		{
			alert('Quantity can\'t be negative');
			return false;
		}
	});
	
});

function init_customercenter ()
{
	/* CUSTOMERCENTER */
	
	$('.edit').click (function () 
	{
		if (confirm ("Are you sure you want to add the products in this order to cart?"))
		{
			$order_id = $(this).parents().children('.order_id').text();
			
			$.post('/customer-center/edit', { order_id: $order_id }, function (response)
			{
				window.location = "/shopping-cart";
			});
		}
	});
	
	$('.detail').click (function () 
	{
		$order_id = $(this).parents().children('.order_id').text();
		
		$.post('/customer-center/detail', { order_id: $order_id }, function (response)
		{
			$.fancybox({
				'opacity' : true,
				'autoScale': true,
				'transitionIn'	: 'elastic',
				'transitionOut'	: 'elastic',
				'content': response
			});
		});
	});
	
	$('.remove').click (function () 
	{
		$order_id = $(this).parents().children('.order_id').text();
		
		if (confirm ("Are you sure you want to remove " + $order_id + "?"))
		{
			$.post('/customer-center/remove', { order_id: $order_id }, function (response)
			{
				$('#contentContainer').html(response);
			});
		}
	});
	
	$('.edit-company').click (function ()
	{
		if ($(this).prev().attr ('type'))
		{
			text = $(this).prev().val();
			type = $(this).attr ('id');
			$.post('/customer-center/edit-company', { type: type, value: text }, function (response) {});
			$(this).prev().remove();
			$(this).prev().after('<span>' + text + '</span>');
			$(this).text('edit');
		}
		else
		{
			text = $(this).prev().text();
			$(this).prev().remove();
			$(this).prev().after('<input type="text" value="' + text + '" />');
			$(this).text('save');
			$(this).prev().focus();
		}
	});
}


function setGalleryType (type)
{
	galleryType = type;
}

function view_address ()
{
	address_to_replace=document.getElementById("e").firstChild;
	real_address=address_to_replace.nodeValue.replace("[at]", "@");
	address_to_replace.nodeValue=real_address;
	address_to_replace.parentNode.setAttribute("href", "mailto:"+real_address);
}

function formatTitle (title, currentArray, currentIndex, currentOpts)
{
	if (galleryType == 'image')
	{
		var content = '<div id="tip7-title">' + (title && title.length ? title : '' ) + '<span class="image_count">Image ' + (currentIndex + 1) + ' of ' + currentArray.length + '</span> <input id="send_picture" type="button" value="Send me this picture" name="send_picture" onclick="send_picture('+ (currentIndex + 1) +');" /> </div>';
	}
	else
	{
		var content = '<div id="tip7-title">' + (title && title.length ? title : '' ) + '<span class="image_count">Image ' + (currentIndex + 1) + ' of ' + currentArray.length + '</span> </div>';
	}
	
	return content;
}

function send_picture (index)
{
	location.href = location.pathname + '/send-picture/' + index;
}
