function thisMovie(movieName) 
	{
	if (navigator.appName.indexOf("Microsoft") != -1) 
		{return window[movieName];}
	else
		{return document[movieName];}
	}

makelogo();
$(window).resize(makelogo);

$(document).ready(function () 
	{
	//contest
	if ($('a.openrecapcha').length)
		{
		$('a.openrecapcha').click(function(){
			var src = this.href;
			$.modal('<iframe src="' + src + '" height="300" width="450" style="border:0">', {
				closeHTML:"",
				containerCss:{
					backgroundColor:"#fff",
					borderColor:"#fff",
					height:310,
					padding:0,
					margin: 0,
					width:470,					
					},
				overlayClose:true, 
				onClose: function(){window.location.reload();$.modal.close();}
				});
			return false;
			});
		
		}
	
	//logo

	//flash
	if ($('#jeepbaner'))
		{
		$('.catalog-nav a[id]').mouseenter(function(){
			thisMovie('jeepembed').showPart(this.id);
			});
		$('.catalog-nav a[id]').mouseleave(function(){
			thisMovie('jeepembed').hidePart(this.id);
			});			
		
		$('#jeepbaner').mouseenter(function(){
			$('.tabs .jeep').parent().addClass('active');
			})

		$('#jeepbaner').click(openjeep);
		$('#jeeptab a').click(openjeep);

		$('#kvadrobaner').click(openkvadro);
		$('#kvadrotab a').click(openkvadro);

		$('#otherbaner').click(openother);
		$('#othertab a').click(openother);		
		
		$('#jeepbaner').mouseleave(function(){
			$('.tabs .jeep').parent().removeClass('active');
			})			
		$('#jeepflash').flash({
    		src: '/img/jeep.swf?z12',
    		width: '100%',
    		id: 'jeepembed',
    		height: 241,
    		wmode:"opaque"
			});
		$('#kvadroflash').flash({
    		src: '/img/quadro.swf?z12',
    		width: '100%',
    		id: 'kvadroembed',
    		height: 241,
    		wmode:"opaque"
			});
		}

	if ($('#kvadrobaner'))
		{
		$('#kvadrobaner').mouseenter(function(){
			$('.tabs .kvadro').parent().addClass('active');
			})
		$('#kvadrobaner').mouseleave(function(){
			$('.tabs .kvadro').parent().removeClass('active');
			})			
		}	

	if ($('#otherbaner'))
		{
		$('#otherbaner').mouseenter(function(){
			$('.tabs .other').parent().addClass('active');
			})
		$('#otherbaner').mouseleave(function(){
			$('.tabs .other').parent().removeClass('active');
			})			
		}			
	//popups	
	if ( $('a#profilelink') && !$('a#profilelink').hasClass('noanon') )
		{
		$('a#profilelink').click(function (e){
			e.preventDefault();
			$('#basic-modal-content').modal();
			return false;
			});
		}
	if ($('a#passchange'))
		{
		$('input[type=radio][name=newpass]').change(function(){
			$('#newpassuser').val('');
			});
		$('#newpassuser').focus(function(){
			$('input[type=radio][name=newpass]').removeAttr("checked");
			});
		$('a#passchange').click(function (e){
			e.preventDefault();
			$('#basic-modal-content').modal();
			return false;
			});
		}
	if ($('.perpageselectorcur'))
		{
		$('.perpageselectorcur').click(function(){
			$('.perpageselector').slideToggle('fast');
			});
		}
		
	//mark already comparing in catalog list
	if (typeof comps != 'undefined')
	{
		for (i=0; i<comps.length; i++){
			if ($(".compare_"+comps[i])){
				$("#compare_"+comps[i]).removeClass('compare').addClass('delcompare');
				$("#compare_"+comps[i]).attr('title', 'Удалить из сравнения');
			}
		}
	} 		
				
	//catalog click handler
	if ($('a.haschildren-close'))
		{
		bs = $('.menudiv ul li ul li a.haschildren-close');		
		for (i = 0; i < bs.length; i++)
			{$(bs[i]).click(catalogopener);} 
		}
				
	//open current in catalog
	if ($('.menudiv li.active'))
		{
		mparent = $('.menudiv li.active').parent('ul');		
		if(mparent.is(':hidden'))
			{
			mparent.show();
			$('a.haschildren-close',mparent.parent('li')).addClass('haschildren-open').removeClass('haschildren-close');
			}
		mmparent = $('.menudiv li.active').parent('ul').parent('li').parent('ul');		
		if(mmparent.is(':hidden'))
			{
			mmparent.show();
			$('a.haschildren-close',mmparent.parent('li')).addClass('haschildren-open').removeClass('haschildren-close');
			}

		}
				
	//compare click handler
	$('.compare, .delcompare').click(function(){
		a = this.id.split('_');
		if ($(this).hasClass('compare')){
			$.post("/compare/add", { item: a[1] });
			$(this).removeClass('compare').addClass('delcompare');
			$(this).attr('title', 'Удалить из сравнения');
			$('#compareok-'+a[1]).show();
			setTimeout(function() {$('#compareok-'+a[1]).fadeOut('slow');}, 3000)				
			return false;
		}
		else{
			
			if ($(this).hasClass('delfromcompare'))
				{$.post("/compare/del", { item: a[1] }, function(){window.location.reload();});}
			else
			{
				$.post("/compare/del", { item: a[1] });
				$(this).removeClass('delcompare').addClass('compare');
				$(this).attr('title', 'Добавить в сравнение');
				$('#comparedelok-'+a[1]).show();
				setTimeout(function() {$('#comparedelok-'+a[1]).fadeOut('slow');}, 3000)				
			}
		return false;
		}
	});
	
	$('.camera, .cameraactive').click(function(){
		a = this.id.split('_');
		if ($(this).hasClass('camera')){
			$('.cameraactive').removeClass('cameraactive').addClass('camera');
			$(this).removeClass('camera').addClass('cameraactive');
			$('.item_image').hide();
			$('#image_'+a[1]).show();
		}
		return false;
	});
	
	
	//add click handler
	$('.add').click(function(){
		if (this.href){
				a = this.id.split('_');
				itemid = a[1];
				if (itemid)	
					{href = this.href;}
				else
					{return true;} //dirty hack for mainpage
			}
		else{
			itemid = $("#item").val();
			href = $(this).parent().attr('action') + '?item=' + $("#item").val() + '&quan=' + $("#quan").val();
		}
		$.get(href, function(){
			$('#addok-'+itemid).show();
			setTimeout(function() {$('#addok-'+itemid).hide();}, 3000)
		});
		return false;
	});

	//town selecter to handler
	$('#town').change(function(){
		$.get("/delivery/savetown?town=" + this.value, function(data){window.location.reload();});
		return false;
	});		
	
	
	if ($('#assistform'))
	{
		setTimeout(function() {$('#assistform').submit();}, 3000) 
	}	
});

function makelogo()
	{
	if($(window).width() > 1250)
		{$('.logo a').addClass('big');}
	else
		{$('.logo a').removeClass('big');}
	}

function closeallcatalog()
	{
	$('.menudiv ul li ul li ul').hide();
	$('.menudiv a.haschildren-open').addClass('haschildren-close').removeClass('haschildren-open');
	}

function catalogopener()
	{
	subnodes = $('#'+this.id+' ~ ul');
	
	if(subnodes.is(':hidden'))
		{subnodes.show('fast');}
	else
		{subnodes.hide('fast');}

	if ($(this).hasClass('haschildren-open'))
		{
		$(this).removeClass('haschildren-open');
		$(this).addClass('haschildren-close');
		}
	else
		{
		if($('ul li ul li:has(ul li #'+this.id+')').length)
			{
			$('ul li ul li:has(ul li #'+this.id+') li ul').hide();
			$('ul li ul li:has(ul li #'+this.id+') li a.haschildren-open').addClass('haschildren-close').removeClass('haschildren-open');						
			}
		else
			{closeallcatalog();}
		$(this).removeClass('haschildren-close');
		$(this).addClass('haschildren-open');
		}	
	return false;
	}

function closealltab()
	{
	$('.tabs .tab').removeClass('active');
	$('#banners ul').hide();
	$('#jeepflash').hide();
	$('#otherflash').hide();
	$('#kvadroflash').hide();
	}

function openjeep()
	{
	closealltab();	
	$('#jeepflash').show();
	$('#jeeptab').addClass('active');
	setTimeout("$('#jeeptab').addClass('active');", 500);	
	return false;
	}

function openkvadro()
	{
	closealltab();		
	$('#kvadroflash').show();
	$('#kvadrotab').addClass('active');
	setTimeout("$('#kvadrotab').addClass('active');", 500);	
	return false;
	}

function openother()
	{
	closealltab();		
	$('#otherflash').show();
	$('#othertab').addClass('active');
	setTimeout("$('#othertab').addClass('active');", 500);	
	return false;
	}	
	
function onPartClicked(partName) 
	{
	window.location = $('a#'+partName).attr('href');
	}
	 
function onPartOver(partName) 
	{	
	$('#flashtext').html($('a#'+partName).html());
	$('#flashover').show();
	if ($('a#'+partName))
		{$('a#'+partName).css('color','#00b2d9');}
	}
	 
function onPartOut(partName) 
	{
	$('#flashover').hide();
	if ($('a#'+partName))
		{$('a#'+partName).css('color','');}	
	}
