// SEFI

$(document).ready(function()
{
	// initializing address plugin
	initAddress();
	
	// initializing tabs
	initTabs();
	
	// initializing highlights
	initHighlights();
	
	// initializing search
	initSearch();
	
	// initializing cart
	initCart();
	
	// init cart total
	initCartTotal();
	
	// initializing tile lists
	initTiles();
	
	// init buttons
	initButtons();
	
	// init comments
	initComments();
	
	// init forms
	initForms();
	
	// init sort orders icons
	initSortOrders();
	
	// init popups
	initPopups();
});


//-----------------------------------------------------------------------
// Address
//-----------------------------------------------------------------------

function initAddress()
{
	// For forward and back
	$.address.change(function(event){
  		$("#tabs").tabs("select" , window.location.hash);
  	});
	
	// when the tab is selected update the url with the hash
	$("#tabs").bind("tabsselect", function(event, ui) { 
  		window.location.hash = ui.tab.hash;
  	});
}


//-----------------------------------------------------------------------
// Tabs
//-----------------------------------------------------------------------

function initTabs()
{
	$("#tabs").tabs();
}


//-----------------------------------------------------------------------
// Highlights
//-----------------------------------------------------------------------

function initHighlights()
{
	$(".highlight").each(function(i) 
	{
    	$(this).append('<div class="highlight-corner tl"></div><div class="highlight-corner tr"></div><div class="highlight-corner bl"></div><div class="highlight-corner br"></div>');
   	});
}

//-----------------------------------------------------------------------
// Search
//-----------------------------------------------------------------------

function initSearch()
{
	// registering event handlers
	// $("#search-button").click(searchButtonClickHandler);
	
	// clearing search
	$("#query").val('');
}

var searchPosInit = null;
function searchButtonClickHandler(event)
{
	if (searchPosInit == null)
	{
		// memorizing initial position
		searchPosInit = $("#search-box").css("left");
	}
	
	var searchPosFinish = 0;
	if ($("#search-box").css("left") != searchPosInit)
	{
		// executing search or hiding search box
		if ($("#query").val() != '')
		{
			// performing search
			searchHandler();
		}
		else
		{		
			// closing search box
			searchPosFinish = searchPosInit;
			$("#search-box").animate({left: searchPosFinish}, {duration: 400});
			
			// preventing default
			event.preventDefault();
		}
		
		return;
	}

	// opening search box
	searchPosFinish =  $("#cart-handle").outerWidth();
	$("#search-box").animate({left: searchPosFinish + "px"}, {duration: 400, complete: searchAnimationCompleteHandler});
	
	// preventing default
	event.preventDefault();
}

function searchAnimationCompleteHandler()
{
	// setting focus
	$("#query").attr('disabled', false);
	$("#query").focus();
}

// performs search request
function searchHandler()
{
	// clearing search
	// $("#query").val('');
	// $("#query").attr('disabled', true);
}


//-----------------------------------------------------------------------
// Cart
//-----------------------------------------------------------------------

function initCart()
{
	// registering event handlers
	$("#cart-button").click(cartButtonClickHandler);
}

var isCartOpened = null;
function cartButtonClickHandler(event)
{
	// preventing default
	event.preventDefault();
	
	if (isCartOpened == null)
	{
		isCartOpened = false;
	}
	
	if (!isCartOpened)
	{
		// updating UI
		cartBeforeShowHandler();
	}
	
	// animating cart
	isCartOpened = !isCartOpened;
	$("#cart").slideToggle({duration: 400, complete: cartAnimationCompleteHandler});
}


function cartAnimationCompleteHandler()
{
	// updating UI
	if (!isCartOpened)
	{
		cartHideHandler();
	}
	else
	{
		cartShowHandler();
	}
}

function cartBeforeShowHandler()
{
	// updating ui
}

function cartShowHandler()
{
	// updating ui
}

function cartHideHandler()
{
	// updating ui
}


//-----------------------------------------------------------------------
// Cart total function
//-----------------------------------------------------------------------
function initCartTotal()
{
	// recalculating first time
	recalculateTotal();
	
	$(".table.cart .qty-input").change(function(event)
	{
		recalculateTotal();
	});
}

function recalculateTotal()
{
	// recalculating total
	$(".table.cart").each(function(i)
	{
		var total = 0;
		$(this).find("tr").each(function(i)
		{
			// skipping caption
			if (i == 0)
			{
				return;
			}
			
			var qty = parseInt($(this).find(".qty-input").val());
			var price = parseFloat($(this).find(".item-price").html());
			
			// updating item cost
			$(this).find(".item-cost").html(qty * price);
			
			// updating total
			total += qty * price;
		});
	
		$(this).parent().find(".cart-total").html(total);
	});
}


//-----------------------------------------------------------------------
// Tile lists
//-----------------------------------------------------------------------

function initTiles()
{
	$(".tilelist").tilelist();
}


//-----------------------------------------------------------------------
// Buttons
//-----------------------------------------------------------------------

function initButtons()
{
	$(".button").each(function(i) 
	{
    	$(this).append('<span class="button-start"></span>');
   	});
}


//-----------------------------------------------------------------------
// Comments
//-----------------------------------------------------------------------

function initComments()
{
	$(".comments-list li").each(function(i) 
	{
    	$(this).append('<div class="arrow"></div>');
   	});
}


//-----------------------------------------------------------------------
// Forms
//-----------------------------------------------------------------------

function initForms()
{
	// init qty forms
	initQtyForms();
}

function initQtyForms()
{
	$(".qty-form").each(function(i) 
	{
    	$(this).append('<div class="qty-arrow up"></div><div class="qty-arrow down"></div>');
   	});
	
	// registering click handlers
	$(".qty-form .qty-arrow.up").click(function(event)
	{
		// incrementing qty value
		var qtyInput = $(event.target).parent(".qty-form").children(".qty-input");
		qtyInput.val(parseInt(qtyInput.val()) + 1);
		qtyInput.change();
	});
	
	$(".qty-form .qty-arrow.down").click(function(event)
	{
		// decrementing qty value
		var qtyInput = $(event.target).parent(".qty-form").children(".qty-input");
		if (parseInt(qtyInput.val()) <= 1)
		{
			// default qty
			qtyInput.val(1);
		}
		else
		{
			qtyInput.val(parseInt(qtyInput.val()) - 1);
		}
		qtyInput.change();
	});
}

//-----------------------------------------------------------------------
// Sort oder icons
//-----------------------------------------------------------------------

function initSortOrders()
{
	$(".sort-order").click(function()
	{
		$(this).toggleClass("reverse");
	});
}


//-----------------------------------------------------------------------
// Init popups
//-----------------------------------------------------------------------

function initPopups()
{
	// building popups
	$(".popup").each(function(i)
	{
		$(this).wrapInner('<div class="popup-side l"><div class="popup-side r"><div class="popup-content"></div></div></div>');
		$(this).prepend('<div class="popup-cap t"><div class="popup-corner lt"></div><div class="popup-corner rt"></div></div>');
		$(this).append('<div class="popup-cap b"><div class="popup-corner lb"></div><div class="popup-corner rb"></div></div><div class="popup-close"></div>');
		
		// registering event listeners
		$(this).find(".popup-close").click(function(e)
		{
			closeAllPopups();
		});
		$("a[href='#" + $(this).attr("id") + "']").click(popupLinkClickHandler);
	});
}

// hides all popups
function closeAllPopups()
{
	$(".popup").css({display: 'none'});
}

// popup link click handler function
function popupLinkClickHandler(e)
{
	// preventing link click
	e.preventDefault();
	
	var popupId = $(e.target).attr("href");
	if (!popupId)
	{
		// error
		return;
	}
	
	// closing all popups
	closeAllPopups();
	
	// positioning popup
	
	// calculating initial coordinates
	var popupX = e.pageX - 20;
	var popupY = e.pageY - 20;
	var popupWidth = $(popupId).width();
	var popupHeight = $(popupId).height();
	
	// correcting coordinates
	var screenWidth = $(window).width();
	var screenHeight = $(document).height();
	var screenMargin = 50;
	
	if (popupX + popupWidth > screenWidth - screenMargin)
	{
		popupX -= popupX + popupWidth - screenWidth + screenMargin;
	}
	if (popupY + popupHeight > screenHeight - screenMargin)
	{
		popupY -= popupY + popupHeight - screenHeight + screenMargin;
	}
	
	$(popupId).css({left: popupX, top: popupY});
	
	// showing popup
	if (!$.browser.msie)
	{
		$(popupId).fadeIn(300);
	}
	else
	{
		$(popupId).css({display: 'block'});
	}
}
