// Show case

//--- Configuration ---//
var im_pos = '39px 30px';
var defbg_poz = '';
var itemParams = {
	width: "200px",
	height: "200px"
}
var cardParams = {
	width: "681px",
	height: "500px"
}
//--- End Configuration ---//

jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
    return this;
}


 $(document).ready(function() { 
    // Show cart
    //$('#catalog .item').click(addEventsToCards);
    // Paginator
    //$('.pages a').click(getPage);
	// Filter slider
    $('#slide a').click(slideFilter);
	$('.scroll-pane').jScrollPane({showArrows:true, scrollbarWidth:19, dragMaxHeight:43});
	// Search field
	$('#searchinput').click(function() {
		if (this.value=='Поиск') this.value = '';
	});
	$('#searchinput').focusout(function() {
		if (this.value=='') this.value = 'Поиск';
	});
	// AJAX login
	$('#onlogin').click(showLoginForm);
	$('#onlogin').attr('onclick','');
	// AJAX Inform
    $('div.inform a.pop').click(showInform);
	// CHint
    $('.related li').mouseover(showCHint);
    $('.related li').mouseleave(hideCHint);
	// IHint
    $('.inform li').mouseover(showCHint);
    $('.inform li').mouseleave(hideCHint);
	// Property Hint
    $('tr.hint td').mouseover(showPHint);
    $('tr.hint td').mouseleave(hidePHint);
	// Banner Hint
    $('div.r').mouseover(showCHint);
    $('div.r').mouseleave(hideCHint);
	// Inform hint
    $('a.infarrl').click(informBack);
    $('a.infarrr').click(informNext);
	// Card description hint
    $('.cardetail .desc').mouseover(function() {
		if ($(this).children('div.cdhint').html()) {
			$(this).children('div.cdhint').show();
			$('.item .price i').attr('style','display:none;');
		}
	});
    $('.cardetail .desc').mouseleave(function() {
		if ($(this).children('div.cdhint').html()) {
			$(this).children('div.cdhint').hide();
			$('.item .price i').attr('style','display:inline;');
		}
	});
	
	
	
	showArrows();
	var ua = window.navigator.userAgent.toLowerCase();
	if (ua.indexOf("msie 6")!= -1) {
		// panel
		$('#panel .filterparams ul.hmenu li ul').mouseover(function() {
			$(this).children('li').show();
			$(this).addClass('hover');
		});
		$('#panel .filterparams ul.hmenu li ul').mouseleave(function() {
			$(this).children('li').hide();
			$(this).children('li.current').show();
			$(this).removeClass('hover');
		});
		// sorting
		$('div.sorts .box ul').mouseover(function() {
			$(this).children('li').show();
			$(this).addClass('hover');
		});
		$('div.sorts .box ul').mouseleave(function() {
			$(this).children('li').hide();
			$(this).children('li.current').show();
			$(this).removeClass('hover');
		});
		// select in cart
		$('div.select ul').mouseover(function() {
			$(this).children('li').show();
			$(this).addClass('hover');
		});
		$('div.select ul').mouseleave(function() {
			$(this).children('li').hide();
			$(this).children('li.current').show();
			$(this).removeClass('hover');
		});
	}
	
	var loginForm = $('#loginform').html();
	$('#loginform').remove();
	$('body').append('<div id="loginform" style="display:none;">'+loginForm+'</div>');
});

function informNext() {
	$('.is1').hide();
	$('.is2').show();
	return false;
}
function informBack() {
	$('.is2').hide();
	$('.is1').show();
	return false;
}

function showInform() {
	var height = alertSize();
	var url = $(this).attr('href')+'&ajax=1';
	var content = getPage(url);
	//$(this).attr('href','#');
	$('#lbOverlay').attr('style','height:'+height+'px;opacity:0.7;');
	$('#lbOverlay').show();
	
	$('#lbDetailsContainer').hide();
	$('#lbImageContainer').hide();
	$('#lbHoverNav').hide();
	$('#lbLoading').hide();
	
	$('#lbOuterContainer').attr('style','height:580px;width:600px;');
	
	$('#lbOuterContainer').append('<div id="lbContent">'+content+' <a href="#" id="closelbCintent"><img src="/themes/nasonpearl/i/close_grey.png" /></a></div>');
	
	$('#lbMain').center();
	$('#lbMain').show();
	
	$('#lbMain').click(function() {
		$('#lbMain').hide();
		$('#lbOverlay').hide();
		
		$('#lbDetailsContainer').show();
		$('#lbImageContainer').show();	
		$('#lbLoading').show();
		$('#lbHoverNav').show();
		$('#lbContent').remove();
	});
	return false;
}

function setChoose(field,arr) {
	$('#OrderValue_'+field+'').change(function() {
		hintText = arr[$(this).val()];
		if (hintText)
			$('.payingtype').html(hintText);
		else $('.payingtype').html('')
		//alert(hintText);
	});
}

function showLoginForm() {
	var height = alertSize();
	
	$('#lbOverlay').attr('style','height:'+height+'px;opacity:0.7;');
	$('#loginform').center();
	$('#loginform').show();
	$('#lbOverlay').click(function() {
		$('#loginform').hide();
		$('#lbOverlay').hide();
	});
	$('#closelbCintent').click(function() {
		$('#loginform').hide();
		$('#lbOverlay').hide();
	});
	//alert('Show login form!\n'+height); 
	return false;
}
function alertSize() {
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
	//Non-IE
	myWidth = window.innerWidth;
	myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth
	|| document.documentElement.clientHeight ) ) {
	//IE 6+ in 'standards compliant mode'
	myWidth = document.documentElement.clientWidth;
	myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	//IE 4 compatible
	myWidth = document.body.clientWidth;
	myHeight = document.body.clientHeight;
	}
	return myHeight;
}

// Card rollower
function showArrows() {
	$('a.toleft').show(100);
	$('a.toright').show(100);
	
	$('a.toleft').click(prevItems);
	$('a.toright').click(nextItems);
}
function prevItems() {
	var url = $(this).attr('rel');
	var content = getPage(url);
	getItems(content,url);
	return false;
}
function nextItems() {
	var url = $(this).attr('rel');
	var content = getPage(url);
	getItems(content,url);
	return false;
}
function getItems(content,url) {
	
	newArrows(content,url);
	var list = $(content).children('.hmenu').html();
	if ($('.hscroller .hmenu').html()!=list) {
		$('.hscroller .hmenu').hide();
		$('.hscroller .hmenu').html(list);
		$('.hscroller .hmenu').show();
	}
}


function newArrows(content,url) {
	var toleft = $(content).children('a.toleft').attr('rel');
	var toright = $(content).children('a.toright').attr('rel');
	/*
	if (toleft==url) {
		$('a.toleft').hide();
	} else {
		$('a.toleft').show();
	}
	if (toright==url) {
		$('a.toright').hide();
	} else {
		$('a.toright').show();
	}//*/
	$('a.toleft').attr('rel',toleft);
	$('a.toright').attr('rel',$(content).children('a.toright').attr('rel'));
}

// AJAX
function getPage(url) {
	return $.ajax({
		url: url,
		async: false
	}).responseText;
}


// Hints
function showCHint() {
	$(this).children('div.chint').show();
}
function hideCHint() {
	$(this).children('div.chint').hide();
}

function showPHint() {
	$(this).children('i.hint').show();
}
function hidePHint() {
	$(this).children('i.hint').hide();
}

// Filter's
function slideFilter() {
	if (($('#panel')).hasClass("hidefilter")) {
		$('#panel').removeClass("hidefilter");
	} else {
		$('#panel').addClass("hidefilter");
	}
	return false;
}

function getPage2() {
	var page = $(this).attr('href').replace("#","");
	var url = "page" + page + ".html";
	var catalog = $.ajax({
		url: url,
		async: false
	}).responseText;
	$('#catalog .compact').hide(500, function() {
		$('#catalog').html(catalog);
		$('#catalog .item').click(addEventsToCards);
		$('#catalog .compact').show(500,function() {
			var pages = $('#new_pages').html();
			$('.pages').html(pages);
			$('.pages a').click(getPage);
		});
	    defbg_poz = '';
	});
}

 function addEventsToCards() {
			var bg_poz = $(this).css('backgroundPosition');
			if (defbg_poz) {
				$('#catalog .compact').show(500);
				$(this).animate({
					width: itemParams['width'],
					height: itemParams['height'],
					backgroundPosition: defbg_poz
				},500);
				
				$(this).addClass("compact");
				$(this).removeClass("card");
				defbg_poz = '';
			} else {
				defbg_poz = bg_poz
				
				$(this).addClass("card");
				$(this).removeClass("compact");
				$('#catalog .compact').hide(500);
				$(this).animate({
					width: cardParams['width'],
					height: cardParams['height'],
					backgroundPosition: im_pos
				},500);
				// add event for close
			}
 }