var isIE6 = false;

$(document).ready(function(){
	
	initSliders();

	$('a.photo').click(function(){
		
		if (this.title==undefined)
			this.title = '';
		
		show_flyer(this.title,'<img src="'+this.href+'" />');
		
		return false;
	});
	
	
	$('form.ajaxForm').submit(function(){
		
		load_form($(this).attr('action'));
		
		return false;
	})
	
	$('a.ajaxForm').click(function(){
		
		load_form($(this).attr('href'));
		
		return false;
	})
	
	// feedback
	
	$('.footMenu li:eq(1) > a').click(function(){
		load_form(this.href);
		
		return false;
		
	});
	
	// suggests
	//$('form.wherebuy input.search').autocomplete({ ajax_get : getTowns, minchars:2, noresults:'город не найден' });
	$('form[name="agentFilter"] input[name="CITY"]').autocomplete({ ajax_get : getTowns, minchars:2, noresults:'город не найден' });

	//print
	
	$('form.printForm input[type="submit"]').click(function(){
		
		pwind = window.open('','printwindow','width=800,height=600,toolbar=no,menubar=no,scrollbars=yes,resizable=yes');
		$(this).parents('form').submit();
	});
	
});


function initSliders(context) {
	
	if (context==undefined)
		context = '';
	
	$(context+' '+'.cSlider .bar').click(function() {
		
		if ($(this).parent().hasClass('open')) {
			
			$(this).next().slideUp('normal',function(){
				$(this).parent().removeClass('open');		
			});
			
		} else {
			
			$('.acco',$(this).parent().siblings('.open')).slideUp('normal',function() {
				$(this).parent().removeClass('open');
			});
			
			
			$(this).next().slideDown('normal',function(){
				$(this).parent().addClass('open');		
			});
			
		}
		
		
		
	});
	
	$(context+' '+'.cSlider .closeBtn').click(function() {
		$(this).parent().slideUp('normal',function(){
		
		
		$(this).parent().removeClass('open');		
		});
		return false;
	});
	
}

function load_form(action) {
	
		show_loading();
		
		
		
		$.get(action,{mode:'ajax'}, function(data){
		//  title = $('h1.ajaxFlyerTitle',$(data)).html();
		
		  show_flyer('',data);
		  $('#shadow .title .t strong').html($('h1.ajaxFlyerTitle',$('#shadow')).html());
		  $('#shadow form').each(function(){
		  	 if ($(this).hasClass('notAjax')) {
		  	 	act = $(this).attr('action');
		  	 	act = act.replace('&mode=ajax','');
		  	 	$(this).attr('action',act);
		  	 } else {
		  	 	$(this).attr('target','ajaxForm');
		  	 }
		  });
		});
		
		first_run = true;
		$('<iframe id="ajaxFormTarget" name="ajaxForm" width="400" height="200" onload="data_loaded()" class="hidden"></iframe>').appendTo('body');

}


function show_loading() {
	
	var height = Math.max($(document).height(),$('body').height()) + 38;
	
	if($.browser.msie && $.browser.version == '6.0') {
		$('body').prepend('<img onfocus="hidePopup()" class="spawn" src="/bitrix/templates/ufg3/i/_.gif" />');
		
	}

	$('#shadow .flyer').hide();
	$('#shadow .flyer .txt').empty();
			
	$('#shadow').prepend('<div class="loader"><img src="/bitrix/templates/ufg3/i/ajax-loader-7.gif" class="loader" /></div>');
	
	$('#shadow').css('min-height',height+'px').show();
	
	document.documentElement.scrollTop = 170;
	document.scrollTop = 170;
}

function hide_loading() {
	$('#shadow .flyer').show();
	$('#shadow div.loader').remove();
	hide_flyer();
}

first_run = false;
function data_loaded() {
	
	if (!first_run) {
	 var aF = document.getElementById("ajaxFormTarget");
		    var aFD = aF.contentWindow || aF.contentDocument;
		    if (aFD.document) {
		        aFD = aFD.document;
		    }
			
		    html = $(aFD).html();
		    
		    if (html!='') {
		       	title = $('h1.ajaxFlyerTitle',$(aFD)).html();		   	
		    	show_flyer(title,$('body',$(aFD)).html());		    
		    	$('#shadow form').attr('target','ajaxForm');
		    }
	} else {
		first_run = false;
	}
}

function show_flyer(title,content,offset) {
	
	var height = Math.max($(document).height(),$('body').height()) + 38;
	
	if($.browser.msie && $.browser.version == '6.0') {
		$('body').prepend('<img onfocus="hidePopup()" class="spawn" src="/bitrix/templates/ufg3/i/_.gif" />');
		
	}

	
	$('#shadow').css('min-height',height+'px').show();
		
	
	$('#shadow .flyer .txt').html(content);
	$('#shadow .flyer .title .t strong').html(title);
	$('#shadow div.loader').remove();
	$('#shadow .flyer').show();
	
	$(document).bind('keyup',esc_check);

	document.documentElement.scrollTop = 170;
	document.scrollTop = 170;

}

function hide_flyer() {
	
	$('#shadow .flyer .title .t strong').empty();
	$('#shadow .flyer .text').empty();
	$('#shadow').hide();	
	
	if($.browser.msie && $.browser.version == '6.0') {
		$('body .spawn').remove();
		
	}
	$('#ajaxFormTarget').remove();
	
}

function esc_check(evt) {
	
	if (evt.keyCode==27) 
		hide_flyer();
	
}

/* Pseudo-selects */
$(document).ready(function(){
	
	$('.pseudo-form .pseudo-select .r').click(function() {
		
		$('#eSelect').remove();
		select_id = '';
		
		if ($(this).prev().hasClass('expanded')) {
			$(this).prev().removeClass('expanded');
		} else {	
			
			$('.pseudo-select .expanded').removeClass('expanded');
			
			$('<div id="eSelect" class="dropDown"></div>').appendTo('body');
			var offset = $(this).parent().offset();
			
			if (!isIE6) {
				$('#eSelect').css('left',offset.left+4);
				$('#eSelect').css('top',offset.top+22);
				$('#eSelect').css('width',$(this).prev().width()+6);			
			} else {
				$('#eSelect').css('left',offset.left+6);
				$('#eSelect').css('top',offset.top+22);
				$('#eSelect').css('width',$(this).prev().width()+6);
				
							
			}			
			$('#eSelect').html($('.dropDown',$(this).prev()).html());
			
			if (isIE6) {
				$('#eSelect .bottom').css('width',$(this).prev().width()+6);
			}
			
			select_id = $(this).parent().attr('id');
			
			$(this).prev().addClass('expanded');
			
			//$(document).bind('click',closeSelects);
			
		}
	});
	
});

var select_id;

function selectVal(item, selectId) {
	
	var sel_id = selectId ? selectId : select_id;

	var val = item.id.replace(sel_id+'_','');
	
	$('#'+sel_id+' .value span').text(trim($(item).text()));	
	sel_id = sel_id.replace('ps_','');
	
	$('select[name="'+sel_id+'"]').val(val);	
	
	$('.value',$('#'+select_id)).removeClass('expanded');
	$('li[id="'+item.id+'"]',$('#'+select_id)).addClass('selected').siblings().removeClass('selected');
	
	$('#eSelect').remove();
	select_id = '';
	
	
	
}

function ps_hover(item) {
	$('#'+item.id).addClass('active');
//	alert(item.id);
}



function closeSelects() {
	$('#eSelect').remove()
	$('.pseudo-select .expanded').removeClass('expanded');
	$(document).unbind('click',closeSelects);
}

function getTowns(key,cont){ 
   var script_name = '/where/towns.php';
   var params = { 'q':key }
   $.get(script_name,params,
         function(obj){ 
           // obj is just array of strings
           var res = [];
           for(var i=0;i<obj.length;i++){
             res.push({ id:i , value:obj[i]});
           }
           // will build suggestions list
           cont(res); 
         },
         'json');
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}


function show_planner() {
	try {
		var height = Math.max($(document).height(),$('body').height()) + 38;
		
		if($.browser.msie && $.browser.version == '6.0') {
			$('body').prepend('<img onfocus="hidePopup()" class="spawn" src="/bitrix/templates/ufg3/i/_.gif" />');
			
		}
	
		$('#shadow .flyer').hide();
		$('#shadow .flyer .txt').empty();
				
		$('#shadow').prepend('<div id="flashPlanner">Для корректной работы планировщика необходимо, чтобы ваш браузер поддерживал Flash</div>');
		
		$('#shadow').css('min-height',height+'px').show();
		
		// flash post...
		var so = new SWFObject("/upload/help/my_calc.swf", "planner", "700", "480", "8", "#FFFFFF");
			so.addVariable('file','/upload/help/ufg_original.swf');
			so.addParam('wmode','transparent');    
			so.write("flashPlanner");
		
		document.documentElement.scrollTop = 170;
		document.scrollTop = 170;
	} catch (e) {}
}

function close_planer() {
	$('#shadow #flashPlanner').empty().remove();
	hide_flyer();
}

