$(document).ready(function(){
		$('.main > li > ul > li > a').each(function () {
			var l=$(this).text().length;
			var pw=$(this).parent().parent().width();
			if( (l >= 21) ) $(this).parent().addClass('double');
			if( l > 38 ) $(this).parent().addClass('triple');
		});
		$('.sub > li > a').each(function () {
			var l=$(this).text().length;
			var pw=$(this).parent().parent().width();
			if( (l >= 21) ) $(this).parent().addClass('double');
			if( l > 38 ) $(this).parent().addClass('triple');
		});
		$('.gsc-resultsbox-visible').addClass('fleft').addClass('floatfix');
		
		$('img').error(function() {$(this).hide();});

		if($('#testedpeople').length == 0) {$('#testIntro').hide();}

		$('input[name="legal"]').attr('checked',true);
		$('input[name="subscribe"]').attr('checked',true);
		$('#testform > div').shuffle();
		$('#wronganswers').after($('div.bgltorange'));
		$('#catname').after($('div.answer'));
		//if($('#testfailed')) $('#catname').hide();

		//if($('.companyselect').val()==' ') $('.countryselect').after('<input type="text" name="company" />');
		$('.companyselect').change(function(){if($('.companyselect').val()==" ") $('.companyselect').after('<br/><input type="text" class="companyreplacer" name="company" />'); else $('.companyreplacer').remove();});
		$('.companyselect').change();

		$('.videocontainer').click(function(e){e.preventDefault(); newHref=$(this).attr('href').match(/v=([a-zA-Z0-9_-]*)(&([a-z]+)=([a-z0-9_-])+)*?/)[1]; $('.player > embed').remove(); $('.player').append('<embed src="http://www.youtube-nocookie.com/v/'+newHref+'?fs=1&amp;amp;hl=ru_RU&amp;amp;rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385">');});

		newHrefFromUrl=window.location.href.match(/video=([a-zA-Z0-9_-]*)/); if(newHrefFromUrl && (newHrefFromUrl.length > 0)) {$('.player > embed').remove(); $('.player').append('<embed src="http://www.youtube-nocookie.com/v/'+newHrefFromUrl[1]+'?fs=1&amp;amp;hl=ru_RU&amp;amp;rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385">');}

		//$('.banner5').click(function(e){e.preventDefault(); window.location='http://www.bayercropscience.ru/ru/videos.html?video=gc2HQmvXOTA'; });
		//$('.banner6').click(function(e){e.preventDefault(); window.location='http://www.bayercropscience.ru/ru/videos.html?video=1fpnLqK2O24';  });

		$('.rotor').cycle();

		if($.browser.msie && $.browser.version=="6.0") {
		$('input:radio').addClass('noborder');
		$('input:checkbox').addClass('noborder');
		$('img[@src$=png]').pngfix();
		}

		
	    //select all the a tag with name equal to modal
    $('a[name=modal]').click(function(e) {
        //Cancel the link behavior
        e.preventDefault();
        //Get the A tag
        var id = $(this).attr('href');
     
        //Get the screen height and width
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();
     
        //Set height and width to mask to fill up the whole screen
        $('#mask').css({'width':maskWidth,'height':maskHeight});
         
        //transition effect     
        //$('#mask').fadeIn(1000);    
        $('#mask').fadeTo("fast",0.8);  
	 
        //Get the window height and width
        var winH = $(window).height();
        var winW = $(window).width();
               
        //Set the popup window to center
        $(id).css('top',  (winH/2)-($(id).height()/2)-100);
        $(id).css('left', (winW/2)-($(id).width()/2)-170);
     
        //transition effect
        //$(id).fadeIn(2000);
		$(id).show();
     
    });
     
    //if close button is clicked
    $('.window .close').click(function (e) {
        //Cancel the link behavior
        e.preventDefault();
        $('#mask, .window').hide();
    });     
     
    //if mask is clicked
    $('#mask').click(function () {
        //$(this).hide();
        //$('.window').hide();
    }); 	


	
	var config = {
		siteURL		: 'www.bayercropscience.ru',	// Change this to your site
		searchSite	: true,
		type		: 'web',
		append		: false,
		perPage		: 8,			// A maximum of 8 is allowed by Google
		page		: 0				// The start page
	}
	

	// Focusing the input text box:
	$('#s').focus();

	$('#searchForm').submit(function(){
		googleSearch();
		$('.visual').addClass('menusep').addClass('MenuSect').addClass('exvisual').removeClass('visual');
		$('.content').addClass('invisible');
		$('.rightcol').removeClass('shift');
		$('li.first').css('visibility','visible');
		$('.home').css('visibility','visible');
		$('#resultsDiv').addClass('content').removeClass('invisible');

		return false;
	});
	
	
	function googleSearch(settings){
		
		// If no parameters are supplied to the function,
		// it takes its defaults from the config object above:
		
		settings = $.extend({},config,settings);
		settings.term = settings.term || $('#s').val();
		
		if(settings.searchSite){
			// Using the Google site:example.com to limit the search to a
			// specific domain:
			settings.term = 'site:'+settings.siteURL+' '+settings.term;
		}
		
		// URL of Google's AJAX search API
		var apiURL = 'http://ajax.googleapis.com/ajax/services/search/'+settings.type+'?v=1.0&callback=?';
		var resultsDiv = $('#resultsDiv');
		
		$.getJSON(apiURL,{q:settings.term,rsz:settings.perPage,start:settings.page*settings.perPage},function(r){
			
			var results = r.responseData.results;
			$('#more').remove();
			
			if(results.length){
				
				// If results were returned, add them to a pageContainer div,
				// after which append them to the #resultsDiv:
				
				var pageContainer = $('<div>',{className:'pageContainer'});
				
				for(var i=0;i<results.length;i++){
					// Creating a new result object and firing its toString method:
					pageContainer.append(new result(results[i]) + '');
				}
				
				if(!settings.append){
					// This is executed when running a new search, 
					// instead of clicking on the More button:
					resultsDiv.empty();
					resultsDiv.append('<h3>Результаты поиска</h3>');
				}
				
				pageContainer.append('<div class="clear"></div>')
							 .hide().appendTo(resultsDiv)
							 .fadeIn('slow');
				
				var cursor = r.responseData.cursor;
				
				// Checking if there are more pages with results, 
				// and deciding whether to show the More button:
				
				if( +cursor.estimatedResultCount > (settings.page+1)*settings.perPage){
					$('<div>',{id:'more'}).appendTo(resultsDiv).text('Еще результаты...').click(function(){
						googleSearch({append:true,page:settings.page+1});
						$(this).fadeOut();
					});
				}
			}
			else {
				
				// No results were found for this search.
				
				resultsDiv.empty();
				resultsDiv.append('<h3>Результаты поиска</h3>');
				$('<p>',{className:'notFound',html:'Ничего не найдено!'}).hide().appendTo(resultsDiv).fadeIn();
			}
		});
	}
	
	function result(r){
		
		// This is class definition. Object of this class are created for
		// each result. The markup is generated by the .toString() method.
		
		var arr = [];
		
		// GsearchResultClass is passed by the google API
		switch(r.GsearchResultClass){

			case 'GwebSearch':
				arr = [
					'<div class="webResult">',
					'<h2><a href="',r.unescapedUrl,'" target="_blank">',r.title,'</a></h2>',
					'<p>',r.content,'</p>',
					'<a href="',r.unescapedUrl,'" target="_blank">',r.visibleUrl,'</a>',
					'</div>'
				];
			break;
			case 'GimageSearch':
				arr = [
					'<div class="imageResult">',
					'<a target="_blank" href="',r.unescapedUrl,'" title="',r.titleNoFormatting,'" class="pic" style="width:',r.tbWidth,'px;height:',r.tbHeight,'px;">',
					'<img src="',r.tbUrl,'" width="',r.tbWidth,'" height="',r.tbHeight,'" /></a>',
					'<div class="clear">[x]</div>','<a href="',r.originalContextUrl,'" target="_blank">',r.visibleUrl,'</a>',
					'</div>'
				];
			break;
			case 'GvideoSearch':
				arr = [
					'<div class="imageResult">',
					'<a target="_blank" href="',r.url,'" title="',r.titleNoFormatting,'" class="pic" style="width:150px;height:auto;">',
					'<img src="',r.tbUrl,'" width="100%" /></a>',
					'<div class="clear">[x]</div>','<a href="',r.originalContextUrl,'" target="_blank">',r.publisher,'</a>',
					'</div>'
				];
			break;
			case 'GnewsSearch':
				arr = [
					'<div class="webResult">',
					'<h2><a href="',r.unescapedUrl,'" target="_blank">',r.title,'</a></h2>',
					'<p>',r.content,'</p>',
					'<a href="',r.unescapedUrl,'" target="_blank">',r.publisher,'</a>',
					'</div>'
				];
			break;
		}
		
		// The toString method.
		this.toString = function(){
			return arr.join('');
		}
	}


});


$(window).resize(function() {
	var w=$(window).width();
	if (w<=950) {
		$(".column1").removeClass('half').addClass('full');
		$(".column2").removeClass('half').addClass('full').removeClass('fright');
		$(".articleimg").removeClass('fright');
		//$(".centralcol").addClass('centralcol800');
		//$(".navigator > div").removeClass('third').addClass('half');			
		$(".navigator > ul > li").removeClass('third').addClass('half');
	} 
	else {
		$(".column1").removeClass('full').addClass('half');
		$(".column2").removeClass('full').addClass('half').addClass('fright');	
		$(".articleimg").addClass('fright');
		//$(".centralcol").removeClass('centralcol800');
		//$(".navigator > div").removeClass('half').addClass('third');				
		$(".navigator > ul > li").removeClass('half').addClass('third');
	} 
});					
