function openWindow(url, w, h) {
	var LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	var TopPosition = (screen.height) ? (screen.height-h)/2-40 : 0;
	window.open(url,'','menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=no,width='+w+',height='+h+',left='+LeftPosition+',top='+TopPosition);
}
$(function(){
	var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);
	if (ie6)
	{
		$('#bag').hide();
	}
	$(document).pngFix();
//	$.preloadCssImages();


	jQuery.facebox.settings.opacity = 0.9;
	jQuery.facebox.settings.closeImage = top_server+'/js/jquery/plugins/facebox/closelabel.gif';
	jQuery.facebox.settings.loadingImage = top_server+'/js/jquery/plugins/facebox/loading.gif';

/*
	$("body").append("<div id='opaque' style='display: none;'></div>");
	$(document).bind('loading.facebox', function() {
		$("#opaque").show();
	});
	$(document).bind('close.facebox', function() {
		$("#opaque").hide();
	});
	$(document).bind('afterReveal.facebox', function() {
	// this is a fix for IE6 which resets the height to 100% of the window height
		$("#opaque").height($(document).height());
	});
*/
	$('#topNav > li').hoverIntent({
		interval:	100,
		over:		topmenuOver,
		timeout:	100,
		out:		topmenuOut
	});
	$('#topCartDiv').hoverIntent({
		interval:	100,
		over:		topCartOver,
		timeout:	100,
		out:		topCartOut
	});
	$('#topCurrenciesDiv').hoverIntent({
		interval:	100,
		over:		topCurrenciesOver,
		timeout:	100,
		out:		topCurrenciesOut
	});
	$('#showFilterBtn').click(function() {
		if ($(this).attr('isHide') == 1)
		{
			$(this)
				.removeClass('f-hide')
				.addClass('f-show')
				.text('SHOW Filters')
				.attr('isHide', 0);
			$('#filters').hide();
			sendAjaxCommand('filters_hide', '');
		}
		else
		{
			$(this)
				.removeClass('f-show')
				.addClass('f-hide')
				.text('HIDE Filters')
				.attr('isHide', 1);
			$('#filters').show();
			sendAjaxCommand('filters_show', '');
		}
		return false;
	});
	$('#dropCeleb li[itemId]').mouseover(function() {
		var $this = $(this);
		if ($this.attr('itemPic') == 1)
		{
			$('#dropCeleb img').attr('src', top_server+'/files/celebrities/'+$(this).attr('itemId')+'_big.jpg');
		}
	});

	$('#newsletterDiv')
		.find('input')
			.attr('itemVal', 'Your email address')
			.val('Your email address')
			.focus(function() {
				if(this.value==$(this).attr('itemVal')) this.value='';
			})
			.blur(function() {
				if(this.value=='')this.value=$(this).attr('itemVal');
			})
		.end()
		.find('a.b-signup')
			.click(function() {
				var $inp = $(this).closest('div').find('input').eq(0);
				if ((trim($inp.val()).length > 0) && ($inp.val() != $inp.attr('itemVal')))
				{
					$.ajax({
						type:		'POST',
						url:		top_server+'/newsletter.php',
						data:		'email='+trim($inp.val()),
						dataType:	'json',
						success:	function (data, textStatus) {
									$inp.val($inp.attr('itemVal'));
									alert('Thank you! You will now receive our newsletter!');
								}
					});
				}
				return false;
			});

	$('#searchDiv')
		.find('input')
			.autocomplete(top_server+'/search.php?a=autocomplete', {
					selectFirst: false,
					containerId: 'search-drop',
					width: '280',
					extraTopShift: 6,
					highlight: function(value, term) {
						return '<a>'+jQuery(value).find('span:first').html(jQuery(value).find('span:first').html().replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + term.replace(/([\^\$\(\)\[\]\{\}\*\.\+\?\|\\])/gi, "\\$1") + ")(?![^<>]*>)(?![^&;]+;)", "gi"), '<span class="search-string">$1</span>')).end().html()+'</a>';
					},
					formatItem: function(row, i, max) {
						return '<a href="#"><span>'+row[0]+'</span> <span class="cat">'+row[1]+'</span></a>';
					}
				}).result(function(event, data, formatted) {
					submitSearchForm();
				}).focus(function() {
					if ($(this).val() == $(this).attr('itemValue'))
						$(this).val('');
				}).blur(function() {
					if ($(this).val() == '')
						$(this).val($(this).attr('itemValue'));
				}).trigger('blur')

		.end()
		.find('a.b-search')
			.click(function() {
				submitSearchForm();
				return false;
			});
	$('#rightWhyNotItems a').click(function() {
		sendCartAjaxCommand('add_accessory', 'accessories_id='+$(this).attr('itemId')+'&colors_id='+$(this).attr('itemColorsId'));
		return false;
	});

	$('#topNav ul[rel=frame_shapes] > li[itemPic=1]').hover(
		function() {
			$(this).siblings('li.frame-shape').html('<img src="'+top_server+'/files/frame_shapes/'+$(this).attr('itemId')+'.jpg">');
		},
		function() {
			$(this).siblings('li.frame-shape').html('');
		});

	$('#curr-drop a[itemId]').click(function() {
		$.ajax({
			type:		'GET',
			url:		top_server+'/ajax.php',
			data:		'a=change_currency&id='+$(this).attr('itemId'),
			dataType:	'json',
			success:	function (data, textStatus) {
						document.location.reload();
					}
		});
		return false;
	});

	var $footer_p = $('p,h1,h2,h3,h4', $('div.legal.ftr-text'));
	if ($footer_p.length > 1)
	{
		$footer_p.eq(0).append($('<a href="#">&nbsp;more &raquo</a>')).find('a').click(function() {
			$(this).hide();
			$footer_p.not(':visible').show();
			return false;
		});
		$footer_p.filter(':gt(0)').hide();
	}
});

function topmenuOver() {
	$(this).children('a').addClass('active');
	var $div = $(this).children('div');
	var offset = 0;
	switch ($(this).attr('rel'))
	{
		case 'manwoman':
			offset = $('#topNav').offset().left;
			break;
		case 'help':
			offset = $('#topNav').offset().left+$div.closest('div.bar').width()-$div.width();
			if (offset < 0)
			{
				offset = 0;
			}
			break;
		default:
			offset = $(this).offset().left;
	}
	$div.css({
			'left':		offset+'px',
			'marginLeft':	'0px'
		})
		.show();
}
function topmenuOut() {
	$(this).children('a').removeClass('active');
	$(this).children('div').hide();
}
function topCartOver() {
	$(this).children('div')
		.css({
			'left':		$('#cart').offset().left+'px',
			'marginLeft':	'0px'
		})
		.slideDown(300);
}
function topCartOut() {
	$(this).children('div').slideUp(300);
}
function topCurrenciesOver() {
	$(this).children('div')
		.css({
			'left':		$('#topCurrenciesDiv').prev('li').offset().left+'px',
			'marginLeft':	'0px'
		})
		.slideDown(300);
}
function topCurrenciesOut() {
	$(this).children('div').slideUp(300);
}
function sendCartAjaxCommand(type, query_str, iteration)
{
	if ('undefined' == typeof iteration)
	{
		iteration = null;
	}
	else
	{
		var $el_prod = $('#product-list > div.product-entry[itemIteration='+iteration+'] a.p-add');
//		var loadingText = '<img src="'+top_server+'/imgs/loader-white.gif" /> Loading...';
		var loadingText = '<img src="'+top_server+'/imgs/loader-white.gif" /> ...';
		$el_prod.html(loadingText);
	}

	if (top_is_cart_options == 1)
	{
		query_str += '&is_cart_options=1&cart_options_pos='+top_is_cart_options_pos;
	}
	else if (top_is_cart == 1)
	{
		query_str += '&is_cart=1';
	}
	$.ajax({
		type:		'POST',
		url:		top_server+'/cartt.php?a=ajax&action='+type,
		data:		query_str,
		dataType:	'json',
		success:	function (data, textStatus) {
			if ('undefined' != typeof data)
			{
				if (('undefined' != typeof data.is_redirect) && 
					(data.is_redirect == 1) && ('undefined' != typeof data.url) &&
					(data.url.length > 0))
				{
					document.location = data.url;
				}
				else if (('undefined' != typeof data.update_cart) && 
					(data.update_cart == 1) && ('undefined' != typeof data.cart_html))
				{
					$('#topCartDiv').html(data.cart_html);
					if ('undefined' == typeof data.not_show)
					{
						cartShow(2000, data.cart_remove);
					}
				}
				else if (('undefined' != typeof data.is_continue) && 
					(data.is_continue == 1) && ('undefined' != typeof data.pos) &&
					(parseInt(data.pos) > 0))
				{
					document.location = top_server+'/options/'+data.pos;
				}
				else if (('undefined' != typeof data.error) && (data.error.length > 0))
				{
					$el_prod.text('Add to bag');
					alert(data.error);
				}

				if (('undefined' != typeof data.iteration) && (data.iteration > 0))
				{
					if ($el_prod.length > 0)
					{
						$el_prod.addClass('p-added').text('Added to bag!');
						setTimeout(function() {
							$el_prod.removeClass('p-added').text('Add to bag');
						}, 2000);
					}
				}
			}
		}
	});
}
function cartShow(timeout, not_hide)
{
	if ('undefined' == typeof not_hide)
	{
		not_hide = 0;
	}
	$('#topCartDiv').each(function() {
		var _this = this
		topCartOver.apply(_this);
		if (not_hide == 0)
		{
			setTimeout( function(){ topCartOut.apply(_this); } , timeout);
		}
	});
}

function submitSearchForm()
{
	var val = trim($('#searchDiv input').val());
	if (val.length > 0)
	{
		document.location = top_server+'/search/'+encodeURIComponent(encodeURIComponent(val));
	}
}
function openFaceboxForm(url, closeByClick, offsetTop)
{
	if ('undefined' == typeof closeByClick)
		closeByClick = false;
	if ('undefined' == typeof offsetTop)
		offsetTop = 0;
	$.facebox.settings.closeByClick = closeByClick;
	$.facebox.settings.offsetTop = offsetTop;
	$.facebox(function() {
		jQuery.get(url, function(data) {
			$.facebox(data);
		});
	});
}

function smoothScroll(target)
{
	var $target = $(target);
	if ($target.length > 0)
	{
		$('html,body').animate({scrollTop: $target.offset().top}, {queue: false, duration: 500});
	}
}

function assignTooltips(mask)
{
	if ('undefined' == typeof mask)
	{
		mask = 'a.b-info';
	}
	$(mask).tooltip({
		bodyHandler: function() {
			return $(this).attr('itemText');
		},
		showURL: false,
		extraClass: 'tool-tip'
	}).click(function() {
		return false;
	});
}

function refreshUtilsBar()
{
	$.ajax({
		type:		'POST',
		url:		top_server+'/ajax.php?a=refresh_utils_bar',
		data:		'',
		dataType:	'json',
		success:	function (data, textStatus) {
					if (('undefined' != typeof data) && ('undefined' != typeof data.div))
					{
						$('#utils-bar[rel=topBar]').html(data.div);
					}
				}
	});
}
function addPrescription(pos)
{
	editPrescription(0, pos);
}
function editPrescription(id, pos)
{
	openFaceboxForm(top_server+'/prescriptions.php?a=pedit&id='+id+'&pos='+pos, false, 20);

}
function closePrescription()
{
	jQuery(document).trigger('close.facebox');
}

