var allowHide=false;

var curProportions=new Array(64, 64);
var curIcon='';
var selectorOpened=false;
var allowHideSelector=false;
var allowHideSubscription=false;
var allowHideInfo=true;
var genImage='';
var imageTimeout=0;
var bodyShown=false;
var curLicense='';
var curMinSize=0;
var curMaxSize=0;
var lastIcon='';
var angleTimeout=false;
var allowRotate=true;
var allowHideFollow=true;

var imageObj = new Array();

$(document).ready(function(){
	$('#preview').rotate({angle:rotateAngle});
	$(document).click(function(){
		if(allowHideFollow && !$('div.menu div.follow').is(':hidden'))
			$('div.menu div.follow').animate({height:'toggle', opacity:'toggle'}, 300);
	});
	
	$('#rotateUp').mousedown(function(){
		rotateAngle=parseInt(rotateAngle)+1;
		
		if(rotateAngle>359 || rotateAngle<-359){
			rotateAngle=0;
		}
		
		$('#rotateInput').val(rotateAngle);
		$('#preview').rotate({angle:rotateAngle});
		allowRotate=true;
		angleTimeout=setTimeout('rotateIcon(1)', 500);
	});
	
	$('#rotateUp').mouseup(function(){
		allowRotate=false;
	});
	
	$('#rotateUp').mouseout(function(){
		allowRotate=false;
	});

	
	$('#rotateDown').mousedown(function(){
		rotateAngle-=1;
		
		if(rotateAngle>359 || rotateAngle<-359){
			rotateAngle=0;
		}
		
		$('#rotateInput').val(rotateAngle);
		$('#preview').rotate({angle:rotateAngle});
		allowRotate=true;
		angleTimeout=setTimeout('rotateIcon(-1)', 500);
		//alert(rotateAngle);
	});
	
	$('#rotateDown').mouseup(function(){
		allowRotate=false;
	});
	
	$('#rotateDown').mouseout(function(){
		allowRotate=false;
	});
	
	$('#rotateInput').keyup(function(){
		if($(this).val()==''){
			rotateAngle=0;
			$(this).val('0');
		}
		
		rotateAngle=parseInt($('#rotateInput').val());
		
		if(rotateAngle>359 || rotateAngle<-359){
			rotateAngle=0;
		}
		
		$('#rotateInput').val(rotateAngle);
		$('#preview').rotate({angle:rotateAngle});
	});
	
	$('#followUs').click(function(){
		$('div.menu div.follow').animate({height:'toggle', opacity:'toggle'}, 300);
	});
	
	$('div.menu').mouseout(function(){
		allowHideFollow=true;
	});
	
	$('div.menu').mouseover(function(){
		allowHideFollow=false;
	});
	
    $('.advanced_link').click(function(){
        var w=Math.floor($(window).width()/2-$('div.advanced div.advanced_form').width()/2)-$('div.advanced').offset().left;
		if($('div.advanced div.advanced_form').is(':hidden')){
			curLicense=$('#filter_license').val();
			curMinSize=$('#filter_min_size').val();
			curMaxSize=$('#filter_max_size').val();
			
			$('div.advanced div.advanced_form').animate({height:'toggle', opacity:'toggle'}, 300);
		}
    });
	
	$('#advancedApply').click(function(){
	
		if(parseInt($('#filter_min_size').val())>parseInt($('#filter_max_size').val()) || ($('#filter_min_size').val()==0 && $('#filter_max_size').val()==0)){
			alert(texts[15], 300);
		}
		else if(keywords=='')
			$('div.advanced div.advanced_form').animate({height:'toggle', opacity:'toggle'}, 300);
		else{
			$('.icons_content .overlay').css('width', $('.icons_content').width()+'px').css('height', $('.icons_content').height()+'px').animate({opacity:'toggle'}, 300, function(){
				 setFiltering('search_settings', $('#filter_license').val()+'---'+$('#filter_min_size').val()+'---'+$('#filter_max_size').val());
			});
			$('div.advanced div.advanced_form').animate({height:'toggle', opacity:'toggle'}, 300);
		}
	});
	
	$('#advancedCancel').click(function(){
		$('#filter_license').val(curLicense);
		$('#filter_min_size').val(curMinSize);
		$('#filter_max_size').val(curMaxSize);
		
		$('div.advanced div.advanced_form').animate({height:'toggle', opacity:'toggle'}, 300);
	});
	
	$('#advancedDefault').click(function(){
		$('#filter_license').val('0');
		$('#filter_min_size').val('0');
		$('#filter_max_size').val('128');
	});

    
		
	$('.generator_dialog_box .close').click(function(){
		hideDialogBox('div.generator_dialog_box');
	});
	
	$('.dialog_box .close').click(function(){
		hideDialogBox('div.dialog_box');
	});
	
	$('div.full_overlay').click(function(){
		if(!$('div.dialog_box').is(':hidden')){
			hideDialogBox('div.dialog_box');
		}
		else if(!$('div.generator_dialog_box').is(':hidden')){
			hideDialogBox('div.generator_dialog_box');
		}
	});

    $('.search_button').click(function(){
		if($.trim($('.search_input').val()).length<3 || $.trim($('.search_input').val()).length>60){
			alert(texts[14], 300);
			return true;
		}
		else if(parseInt($('#filter_min_size').val())<=parseInt($('#filter_max_size').val()) && !($('#filter_min_size').val()==0 && $('#filter_max_size').val()==0)){
			location.href='/'+identifyLang()+'/search/1/'+$('#filter_license').val()+'/'+$('#filter_min_size').val()+'/'+$('#filter_max_size').val()+'/'+$('.search_input').val();
		}
		else{
			alert(texts[15], 300);
		}
    });
	
	$('.search_input').keypress(function(e){
		var code = (e.keyCode ? e.keyCode : e.which);
		if(code == 13) { //Enter keycode
		   $('.search_button').click();
		}
	});

    var isCtrl = false;

    $(document).keyup(function (e) {
            if(e.keyCode == 17)
                    isCtrl = false;
    }).keydown(function (e) {
            if(e.keyCode == 17)
                    isCtrl = true;

            if(e.keyCode == 37 && isCtrl == true) {
                if($('.paging.previous').attr('href'))
                    location.href=$('.paging.previous').attr('href');
            }
            else if(e.keyCode == 39 && isCtrl == true){
                if($('.paging.next').attr('href'))
                    location.href=$('.paging.next').attr('href');
            }
    });

	$('#searchStore').click(function(){
		var kw=$.trim($('.store_search_input').val());
		if(kw.length<3){
			//alert('too short');
			alert('The minimum length of search phrase must be more than 2 symbols', 300);
			return true;
		}
		else{
			
			var reg = /^([A-Za-z0-9_\-\.\s])+$/;
			
			if(!reg.test(kw))
			{
				alert(texts[5]);
			}
			else
			{
				//errorDiv.style.display='none';
				location.href='/en/store/search/1/'+$('.store_search_input').val();
			}
			
		}	
	});
	
	$('.store_search_input').keypress(function(e){
		var code = (e.keyCode ? e.keyCode : e.which);
		if(code == 13) { //Enter keycode
		   $('#searchStore').click();
		}
	});

    $('#search_form').submit(function(){
        $('.search_button').click();
        return false;
    });
	$('#transparentBackId1').click(function(){
		if($('#typeId').val()!='.JPEG' && $('#typeId').val()!='.BMP' && $('#typeId').val()!='.GIF'){
			$('#backgroundOverlay').fadeIn(300);
		}
	});
	
	$('#transparentBackId0').click(function(){
		if($('#typeId').val()!='.JPEG' && $('#typeId').val()!='.BMP'){
			$('#backgroundOverlay').fadeOut(300);
		}
	});
	
	$('#swap').click(function(){
		var tW=$('#widthId').val();
		$('#widthId').val($('#heightId').val());
		$('#heightId').val(tW);
		return false;
	});
	
	$('.select_type').click(function(){
			allowHideSelector=false; 
		
		$('#typesDiv').animate({height:'toggle', opacity:'toggle'}, 300);
	});
	
	$('.inner_t_div').click(function(){
		var t=$(this).html();
		setType(t);
		
		//$('#typesDiv').animate({height:'toggle', opacity:'toggle'}, 300);
	});
	
	$('#colorizeId0').click(function(){
		$('#foregroundOverlay').fadeIn(300);
	});
	
	$('#colorizeId1').click(function(){
		$('#foregroundOverlay').fadeOut(300);
	});
	
	$('#foregroundTd').click(function(){
		 allowHidePicker=false; 
		 curSelector='foreground'; 
		 $('#colorId').val($('#foregroundId').val()); 
		 displayColorPicker(true);
		 eventFirer(document.getElementById('colorId'), 'change');
	});
	
	$('#backgroundTd').click(function(){
		 allowHidePicker=false; 
		 curSelector='background'; 
		 $('#colorId').val($('#backgroundId').val()); 
		 displayColorPicker(true);
		 eventFirer(document.getElementById('colorId'), 'change');
	});
	
	$('.input_color').keydown(function(){
		if($(this).val().length==6){
			$('#colorId').val($(this).val());
			eventFirer(document.getElementById('colorId'), 'change');
		}
		else if($(this).val().length>6){
			$(this).val($(this).val().substr(0, 6));
		}
	});
	
	$('.input_color').keyup(function(){$(this).keydown()});
	
	$('.input_color').blur(function(){	
		$('#colorId').val($(this).val());
		eventFirer(document.getElementById('colorId'), 'change');
	});
	
	$('#proportionsId1').click(function(){
		constrainProportions(0, $('#widthId').val());
	});
	
	$('#widthId').blur(function(){
		constrainProportions(0, $(this).val());
	});
	
	$('#heightId').blur(function(){
		constrainProportions(1, $(this).val());
	});
	
	$('#resetSize').click(function(){
		$('#widthId').val(curProportions[0]);
		$('#heightId').val(curProportions[1]);
		return false;
	});
	
	$('#resetForm').click(function(){
		if(confirm(texts[9])){
			clearForm();
		} 
		return false;
	});
	
	$('.generate_button').click(function(){
		generateIcon(false);
		displayColorPicker(false);
		if(!$('#typesDiv').is(':hidden'))
			$('#typesDiv').animate({height:'toggle', opacity:'toggle'}, 300);
		return false;
	});
	
	$('.download_button').click(function(){
		generateIcon(true);
		return false;
	});
	
	$('#colorPickerRemover').click(function(){
		if(allowHidePicker){
			displayColorPicker(false)
		}
		else{
			allowHidePicker=true
		};
		
		if(!$('.types_div').is(':hidden') && allowHideSelector){
			$('#typesDiv').animate({height:'toggle', opacity:'toggle'}, 300);
		}
		
		allowHideSelector=true;
		allowRotate=false;
	});
	
	
	//Stats
	$.post(
		"/actions_handler.php",
		{'action':'tags_update', 'keywords':keywords}
	)
	
	function showDialogBox(box){
		$(box).css('left', (($(window).width()/2)-($(box).width()/2))+'px');
        $(box).css('top', (($(window).height()/2)-($(box).height()/2)+$(document).scrollTop())+'px');
		$('div.full_overlay').css('width', $(document).width()+'px').css('height', $(document).height());
        $('div.full_overlay, '+box).fadeIn('fast');	
	}
	
	function hideDialogBox(box){
		$(box+', .full_overlay').fadeOut('fast');
	}
	
	function renewJQuery(){

		$('.icon img').unbind('click');
		$('.icon img').click(function(){
			
			curProportions[0]=$(this).width();
			curProportions[1]=$(this).height();
			
			if($('#widthId').val()=='0')
				$('#widthId').val(curProportions[0]);
			
			if($('#heightId').val()=='0')
				$('#heightId').val(curProportions[1]);
					
			curSelector='foreground';
			$('#foregroundId').val(frg);
			$('#colorId').val(frg);
			$('#foregroundPreviewId').css('background-color', '#'+frg);
			eventFirer(document.getElementById('colorId'), 'change');
			curSelector='background';
			$('#colorId').val($('#backgroundId').val()); 
			eventFirer(document.getElementById('colorId'), 'change');
			
			if($('#proportionsId1').is(':checked'))
				$('#proportionsId1').click();
			else
				$('#proportionsId0').click();
	
			if($('#transparentBackId1').is(':checked'))
				$('#transparentBackId1').click();
			else
				$('#transparentBackId0').click();
	
			if($('#colorizeId1').is(':checked'))
				$('#colorizeId1').click();
			else
				$('#colorizeId0').click();
	
			rotateAngle=$('#rotateInput').val();
			$('#preview').rotate({angle:rotateAngle});
			
			showDialogBox('div.generator_dialog_box');
			
			var oLeft=Math.floor($('#contentDiv2').width()/2-$(this).width()/2);
			var oTop=Math.floor($('#contentDiv2').height()/2-$(this).height()/2);
			
			var size=parseInt($(this).parent().children('div.size').html());
			size=Math.round(size/1024);
			
			$('#imageSizeSpan').html('<b>'+$(this).parent().children('div.size').html()+'</b> B (<b>~'+size+'</b> KB)');
			
			$('#contentDiv2').html('<img src="'+$(this).attr('src')+'" style="margin-left:'+oLeft+'px; margin-top:'+oTop+'px" />');
			curIcon=$(this).parent().attr('id').replace('icon', '');
		});
		
		$('.icon .type').unbind('click');
		$('.icon .type').click(function(){
			$('.icon div.l_info').each(function(){
				if(!$(this).is(':hidden')){
					$(this).fadeOut(300);
				}
			});
				
				
			if($(this).siblings('div.l_info').length>0){
				$(this).siblings('div.l_info').fadeIn(300);
			}
			else{
				//alert($(this).parent.parent.attr('id'));
				$(this).parent().append('<div class="l_info"><div class="close" onclick="$(this).parent().fadeOut(300)"></div><center><div class="loading"><img src="/img/loading.gif" /></center></div></div>');
				
				var l=parseInt($(this).parent().width())+10;
				if(l+$(this).parent().offset().left+280>$(window).width()){
					l=-270;
				}
				
				
				$(this).siblings('div.l_info').css('left', l+'px').fadeIn(300);
				
				var l=$(this).parent().children('div.l_info');
				$.post(
					"/actions_handler.php",
					{'action':'license_info', 'img_id':$(this).parent().attr('id').replace('icon', '')},
					function(data){
						l.css('background-image', 'none');
						l.html('<div class="close" onclick="$(this).parent().fadeOut(300)"></div>'+data);
					}
				);
			}
		});
		
		$('.icon').unbind('mouseover');
		$('.icon').mouseover(function(){
			allowHideInfo=false;
		});
		
		$('.icon').unbind('mouseout');
		$('.icon').mouseout(function(){
			allowHideInfo=true;
		});
		
		$('.icon').parents().unbind('click');
		$('.icon').parents().click(function(){
			if(allowHideInfo){
				$('.icon div.l_info').each(function(){
					if(!$(this).is(':hidden')){
						$(this).fadeOut(300);
					}
				});
			}
			
		});
		
		$('.readme').click(function(){
			$('.dialog_box .loading').show();
			$('.dialog_box .dialog_content').hide();
			showDialogBox('div.dialog_box');
			
			var collection=$(this).attr('id').replace('collection_', '');
			
			$.post(
				"/actions_handler.php",
				{'action':'get_readme', 'collection':collection},
				function(data){
					$('.dialog_box .dialog_content').html(data);
					$('.dialog_box .loading').fadeOut(300, function(){
						$('.dialog_box .dialog_content').fadeIn(300);
					});
				}
			);
		});
	
        $('#doZoom').unbind('change');
        $('#doZoom').change(function(){
            if($(this).val()=='0'){
                removeLens();
            }
            else{
                zoomSize=$(this).val();
                setLens();
            }

            $.post(
				"/actions_handler.php",
				{'action':'set_zoom', 'zoom':$(this).val()}
			);
        });

		$('#iconsPerPage').unbind('change');
		$('#iconsPerPage').change(function(){
			
			$('.icons_content .overlay').css('width', $('.icons_content').width()+'px').css('height', $('.icons_content').height()+'px').animate({opacity:'toggle'}, 300, function(){
				 setFiltering('per_page', $('#iconsPerPage').val());
			});
		});
		
		$('div.bckg').click(function(){
			
			var b=$(this).attr('class').replace('bckg ', '');
			
			$('div.icon').attr('class', 'icon '+b);
            $('#lens').attr('class', b);
			
			$.post(
				"/actions_handler.php",
				{'action':'set_background', 'background':b}
			);
		});
		
		$('div.bckg1').click(function(){
			var b=$(this).attr('class').replace('bckg1 ', '');
			
			$('#contentDiv2').attr('class', b);
		});

        if($('#doZoom').val()!='0'){
            setLens();
        }
	}
	
	function generateIcon(download)
	{
		var foreColor=$('#foregroundId').val();
		var backColor=$('#backgroundId').val();
		var width=$('#widthId').val();
		var height=$('#heightId').val();
		var transparency=$('#transparentBackId1').is(':checked');
		var colorize=$('#colorizeId1').is(':checked');
		var type=$('#typeId').val();
		var rotate=$('#rotateInput').val();

		if(transparency)
			transparency=1;
		else
			transparency=0;
			
		if(colorize)
			colorize=1;
		else
			colorize=0;
		
		if($('#proportionsId1').is(':checked'))
			cProp=1;
		else
			cProp=0;
		
		
		var cont=true;
		if(width>512 || width<5 || height>512 || height<5)
		{
			cont=false;
			$('#contentDiv2').html('<center><div class="error">'+texts[1]+'</center></div>');
		}
        else if(type=='.ICO' && (width>255 || height>255)){
            cont=false;
            $('#contentDiv2').html('<center><div class="error">'+texts[18]+'</center></div>');    
        }

		if(cont)
		{
			t=transparency;
			
			if(download)
			{
				window.frames['generatorFrame'].location.href=genDomain+'/generator.php?image='+curIcon+'&rotate='+rotate+'&fore_color='+foreColor+'&back_color='+backColor+'&size='+width+'x'+height+'&transparency='+t+'&colorize='+colorize+'&type='+type+'&download&class='+$('#contentDiv2').attr('class')+'&proportions='+cProp+'&uncacher='+(Math.random()*100);
			}
			else
			{
				$('#mLoadingDiv2').fadeIn(300);
				$('#contentDiv2').html('');
				$('#imageSizeSpan').html(texts[7]);
				//alert('/generator.php?image='+curIcon+'&fore_color='+foreColor+'&back_color='+backColor+'&size='+width+'x'+height+'&transparency='+t+'&colorize='+colorize+'&type='+type+'&uncacher='+(Math.random()*100));
				
				lastIcon='?image='+curIcon+'&rotate='+rotate+'&fore_color='+foreColor+'&back_color='+backColor+'&size='+width+'x'+height+'&transparency='+t+'&colorize='+colorize+'&type='+type+'&class='+$('#contentDiv2').attr('class')+'&proportions='+cProp+'&uncacher='+(Math.random()*100);
				
				genImageLoader(genDomain+'/generator.php'+lastIcon, 'showImage()');
			}
		}
		
		return false;
	}
	

	function genImageLoader(s, fun)
	{
		clearTimeout(imageTimeout);
		imageTimeout=0;
		genImage = new Image();
		genImage.onload=function (){imageTimeout=setTimeout(fun, 0)};
		genImage.onerror=function (){$('#contentDiv2').html('<div class="error">'+texts[8]+'</div>'); $('#mLoadingDiv2').hide()};
		genImage.src=s;
	}
	
	


	
	function constrainProportions(prop, val)//prop->0=>width; prop->1=>height
	{
		if(!isNaN(val) && val>0)
		{
			if($('#proportionsId1').is(':checked'))
			{
				if(prop)
				{
					h=val;
					w=parseInt(val)*(curProportions[0]/curProportions[1]);
				}
				else
				{
					w=val;
					h=parseInt(val)*(curProportions[1]/curProportions[0]);
				}
				$('#widthId').val(parseInt(w));
				$('#heightId').val(parseInt(h));
			}
		}
		else
		{
			$('#widthId').val('');
			$('#heightId').val('');
		}
	}
	
	function alignIcons(){
		$('.icon img').each(function(){
			$(this).css('margin-top', Math.floor(($(this).parent().height()-30)/2-$(this).height()/2)+'px');
		});
	}
	
    function setFiltering(filter, value){
		$.post(
			"/actions_handler.php?lang="+identifyLang(),
			{'action':'update_filter', 'filter':filter, 'value':value, 'keywords':keywords},
			function(data){
				$('.loaded_content').animate({opacity:'toggle'}, 300, function(){
					$('.loaded_content').html(data);
					$('.icons_content .overlay').animate({opacity:'toggle'}, 300);
					$('.loaded_content').animate({opacity:'toggle'}, 300);
					renewJQuery();
					alignIcons();
					getAdverts();
				});
				
			}
		);
    }

	
	function setSettingsCookie(name, value, expires){
		// set time, it's in milliseconds
		var today = new Date();
		today.setTime( today.getTime() );

		if ( expires )
		{
			expires = expires * 1000 * 60 * 60 * 24;
		}
		var expires_date = new Date( today.getTime() + (expires) );

		document.cookie = name + "=" +escape( value ) +
		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" );
	}
	
	function firstTime(){
	    return true;
		var aAllCookies = document.cookie.split( ';' );
		var aTempCookie = '';
		var cookieName = '';
		var cookieValue = '';
		var b_cookie_found = false; // set boolean t/f default f
		
		for ( i = 0; i < aAllCookies.length; i++ )
		{
			aTempCookie = aAllCookies[i].split( '=' );
			cookieName = aTempCookie[0].replace(/^\s+|\s+$/g, '');
			if ( cookieName == 'isFirstTime' )
			{
				if ( aTempCookie.length > 1 )
				{
					cookieValue = unescape( aTempCookie[1].replace(/^\s+|\s+$/g, '') );
				}
				
				if(cookieValue!='')
				{
					b_cookie_found = true;
				}
				break;
			}
			aTempCookie = null;
			cookieName = '';
		}
		
		if ( !b_cookie_found )
		{
			var h='<center><h1>'+texts[10]+'</h1></center><br /><br /><center><table border="0" class="first_time"><tr><td style="padding-left:10px;"><h2>1. '+texts[11]+'</h2></td><td style="padding-left:10px;"><h2>2. '+texts[12]+'</h2></td><td rowspan="2" style="padding-top:20px;"><img src="/img/3.jpg" class="first_time" align="absmiddle" /></td></tr><tr><td><img src="/img/1.jpg" class="first_time" align="absmiddle" /></td><td><img src="/img/2.jpg" class="first_time" align="absmiddle" /></td></tr></table></center>';
			$('.dialog_box .loading').hide();
			$('.dialog_box .dialog_content').show();
			$('.dialog_box .dialog_content').html(h);
			showDialogBox('.dialog_box');
		}
		
		setSettingsCookie('isFirstTime', true, 30);
	}

	function clearForm()
	{
		curSelector='background';
		$('#backgroundId').val('FFFFFF');
		$('#colorId').val('FFFFFF'); 
		eventFirer(document.getElementById('colorId'), 'change');
		
		curSelector='foreground';
		$('#foregroundId').val('#000000');
		$('#colorId').val('#000000'); 
		eventFirer(document.getElementById('colorId'), 'change');
		
		$('#backgroundPreviewId').css('background','#FFFFFF');
		$('#proportionsId0').click();
		$('#widthId').val(curProportions[0]);
		$('#heightId').val(curProportions[1]);
		setType('.GIF');
		$('#transparentBackId0').click();
		$('#colorizeId1').click();
		$('#contentDiv2').attr('class', 'white');
		$('#rotateInput').val('0');
		rotateAngle=0;
		$('#preview').rotate({angle:rotateAngle});
		
	}
	
	function setType(t){
		
		$('#typeId').val(t);
		$('#typeDisplayId').html(t);
		if(t=='.JPEG' || t=='.BMP')
		{
			$('#transBackOverlay').fadeIn(300);
			
			if(!$('#backgroundOverlay').is(':hidden')){
				$('#backgroundOverlay').fadeOut(300);
			}
		}
		else
		{
			if($('#transparentBackId1').is(':checked') && t!='.GIF'){
				$('#backgroundOverlay').fadeIn(300);
			}
			else if(!$('#backgroundOverlay').is(':hidden')){
				$('#backgroundOverlay').fadeOut(300);
			}
			
			$('#transBackOverlay').fadeOut(300);
		}
	}
	
	function getAdverts(){
		//return true;
		var h=$('.icons_content').height()-107;
		$('.right_store').height(h);
		
		if(keywords!=''){
			var keyPattern = /^[a-zA-Z0-9._-]{2,60}$/;  
			if(!keyPattern.test(keywords))
			{
				var k=keywords.split('collection:');
				k=k[0];
				k=k.replace(' ', '+');
				
				var newScript = document.createElement('script');
				newScript.type = 'text/javascript';
				var source = 'https://www.googleapis.com/language/translate/v2?key=AIzaSyAzcvbKCUyuarhn6gk_kD5sIBtkogq46sA&source=ru&target=en&callback=translateText&q=' + k;
				
				newScript.src = source;
				// When we add this script to the head, the request is sent off.
				document.getElementsByTagName('head')[0].appendChild(newScript);
			}
			else{
				runGetAdverts(keywords);
			}
		}
	}

	
new renewJQuery();
new alignIcons();
new getAdverts();
new firstTime();
});
	
function translateText(response) {
	runGetAdverts(response.data.translations[0].translatedText);
}

function runGetAdverts(keywords){
	$(document).ready(function(){
		h=$('.icons_content').height()-227;

		$.post(
			"/store_adv.php",
			{'keywords':keywords, 'height':h},
			function(data){
				$('.right_store').css('background-image', 'none').html('<h3>'+texts[16]+'</h3>'+data+($.trim(data)!=''?'<a href="/'+identifyLang()+'/store/search/1/'+keywords+'">'+texts[17]+' &raquo;</a>':''));
			}
		);
	});
}


	
function identifyLang(){

	var path=window.location.pathname;
	//.replace('/ru/', '/uz/')
	if(path=='/'){
		path='/en/';
	}

	if(path.indexOf('/ru')==-1){//cur lang - en
		return 'en';
	}
	else{
		return 'ru';
	}
}

function showImage(){
	var curDiv=$('#contentDiv2');
	curDiv.html('');
	curDiv.append(genImage);
	
	curDiv.children('img').css('margin-left', Math.floor(curDiv.width()/2-curDiv.children('img').width()/2)).css('margin-top', Math.floor(curDiv.height()/2-curDiv.children('img').height()/2));
	
	$.get('/file_size.php'+lastIcon, function(data){
		$('#imageSizeSpan').html('<b>'+data+'</b> B (<b>~'+Math.round((data/1024))+'</b> KB)');
	});
	$('#mLoadingDiv2').fadeOut(300);
}

	

function rotateIcon(a){
	if(allowRotate){
		rotateAngle+=a;
		
		if(rotateAngle>359 || rotateAngle<-359){
			rotateAngle=0;
		}
		
		$('#rotateInput').val(rotateAngle);
		$('#preview').rotate({angle:rotateAngle});
		angleTimeout=setTimeout('rotateIcon('+a+')', 10);
	}
}
	
