/**
* Project js Library
*/ 

// vacancies form
function sendVacForm() {
	var fid = 'b-brief-search';
	if($_(fid).style.display == 'none') {
		fid = 'b-full-search';
	}
	
	$_(fid.replace('b-','f-')).submit();
}

// set prog type
function setProgType() {
	if(getCookie('PROG_TYPE') == null || getCookie('PROG_TYPE') == '') {
		setCookie('PROG_TYPE',1,3600*24*365,'/');
		location.href = location.href;
	}else{
		setCookie('PROG_TYPE','',3600*24*365,'/');
		location.href = location.href;
	}
}

// set prog
function setProg(pic,title,slogan,href) {
	$('#b-prog').css('background','url("'+pic+'") no-repeat');
	$('#b-prog-link').attr('href',href).html(title);
	$('#b-prog-descr').html(slogan);
}

// for connect events to the elements
function post_loading(container){

	if ($_('frmAddQA')){
		
		var _always_submit = false;
		$_('frmAddQA').setAttribute('action', '');
		portal.events.attach($_('frmAddQA'), "submit", function (e){
				
				e.returnValue = false;
				e.preventDefault ? e.preventDefault() : '';
					
				if (_always_submit){
					return e.returnValue;
				} else {
					//e.returnValue = athis._checkForm(e, athis);
					
					/*if (!e.returnValue){
						e.preventDefault ? e.preventDefault() : '';
					} else {*/
					_always_submit = true;
					var result = portal.hash.parse();
 		
			 		if (result['id'] || result['url']){
			 			result['form'] = $_('frmAddQA');
			 			result['method'] = 'post';
			 			
			 			portal.events.remove($_('frmAddQA'), "submit");
			 			
			 			portal.ajax.load(null, result, container.insert);
			 			container.clear(e);
			 		}
					//}
					
				}
				return e.returnValue;
			}, false);
			
	}
	
}


/**
* callback funtion for rules agreement
*/
function post_rules_agreement(paid_type, paid_system, popup){
	
	$_('order[paid_type]').value = paid_type;
	$_('order[paid_system]').value = paid_system;
	if (typeof popup != 'undefined'){
		
		$('#btn-notagree').click(function(){
			popup.destroy(popup);
			});
			
		$('#btn-agree').click(function(){
			$('#frmSubmit').submit();
			});
		post_loading(popup);
		
	}
	
} 




/**
* on DOM ready
*/
portal.ready(function (){
	
	// connecting to the input fields events
	/*var fields_defaults = {'search_q' : '',
		'subscribe_email' : '',
		'qa_fio' : '',
		'qa_email' : '',
		's-keys' : '',
		's-keys2' : ''};
	
	for (var i in fields_defaults){
		fields_defaults[i] = $('#'+i).attr('value');
		$('#'+i).bind('focus', function(){
			if ($(this).attr('value') == fields_defaults[$(this).attr('id')])
				$(this).attr('value', '');
			});
		$('#'+i).bind('blur', function(){
			if ($(this).attr('value') == '')
				$(this).attr('value', fields_defaults[$(this).attr('id')]);
			});
		
	
	}
	
		
	// connect to calendar helper
	if ($_('h-calendar')){
		$('#h-calendar').attr('p-href', $('#h-calendar').attr('href')).attr('href', 'javascript:void(0);').click(function(e){
			var popup = new oPopupLayer();
			popup.addParams({'insert_callback' : function (){post_loading(popup);}})
			popup.create();
	 		portal.ajax.load(e, {'url' : $(this).attr('p-href')}, popup.insert);
	 		});
	}*/
	
		
	// connect galleryview and lightbox
	/* if ($_('photos')){
	
		//$('#photos a').lightBox();
		
		$('#photos').galleryView({
			gallery_width: 600,
			gallery_height: 120,
			gallery_padding: 20,
			panel_width: 550,
			panel_height: 367,
			frame_width: 180,
			frame_height: 123,
			pause_on_hover: true,
			show_panels : true,
			show_filmstrip : true,
			frame_opacity: 1
		});	
	} */
	
	// connect countries gallery
	/*if ($_('b-gallery')){
	
		var onMouseOutOpacity = 0.67;
				$('#b-gallery ul.thumbs li').opacityrollover({
					mouseOutOpacity:   onMouseOutOpacity,
					mouseOverOpacity:  1.0,
					fadeSpeed:         'fast',
					exemptionSelector: '.selected'
				});
	
		var gallery = $('#b-gallery').galleriffic({
		        delay:                     7000, // in milliseconds
		        numThumbs:                 4, // The number of thumbnails to show page
		        preloadAhead:              40, // Set to -1 to preload all images
		        enableTopPager:            false,
		        enableBottomPager:         true,
		        maxPagesToShow:             4,  // The maximum number of pages to display in either the top or bottom pager
		        imageContainerSel:         '#slideshow',
				controlsContainerSel:      '#controls',
				captionContainerSel:       '',
				loadingContainerSel:       '#gal-loader',
				renderSSControls:          true, // Specifies whether the slideshow's Play and Pause links should be rendered
		        renderNavControls:         true, // Specifies whether the slideshow's Next and Previous links should be rendered
		        playLinkText:              'Play',
		        pauseLinkText:             'Pause',
		        prevLinkText:              'Previous',
		        nextLinkText:              'Next',
		        nextPageLinkText:          'Next &rsaquo;',
		        prevPageLinkText:          '&lsaquo; Prev',
		        enableHistory:             false, // Specifies whether the url's hash and the browser's history cache should update when the current slideshow image changes
		        enableKeyboardNavigation:  true, // Specifies whether keyboard navigation is enabled
		        autoStart:                 true, // Specifies whether the slideshow should be playing or paused when the page first loads
		        syncTransitions:           true, // Specifies whether the out and in transitions occur simultaneously or distinctly
		        defaultTransitionDuration: 900,
				onSlideChange:             function(prevIndex, nextIndex) {
						// 'this' refers to the gallery, which is an extension of $('#thumbs')
						this.find('ul.thumbs').children()
							.eq(prevIndex).fadeTo('fast', onMouseOutOpacity).end()
							.eq(nextIndex).fadeTo('fast', 1.0);
					},
				onPageTransitionOut:       function(callback) {
						this.fadeTo('fast', 0.0, callback);
					},
				onPageTransitionIn:        function() {
						this.fadeTo('fast', 1.0);
					}
		    });

	}
	
	// attach to the vac list
	if ($('.b-fav').length > 0){
		
	}
		
	// attach to the program docs blocks
	if ($('.b-docs h3').length > 0){
		$('.b-docs h3').click(function(){
			if ($(this).hasClass('act')){
				$('.b-docs .b-form-container').css('display', 'none');
				$(this).removeClass('act');
			} else {
				$('.b-docs .b-form-container').css('display', 'block');
				$(this).addClass('act');
			}
		})
	}
	
	// search form
	if ($('#s-keys').length > 0){
		var s_types = [1, 2];
		$('.b-more-fields').click(function(){
			if ($_('s-type').value == '2'){
				$('#b-full-search').css('display', 'none');
				$('#b-brief-search').css('display', '');
				$_('s-type').value = '1';
			} else {
				$('#b-full-search').css('display', '');
				$('#b-brief-search').css('display', 'none');
				$_('s-type').value = '2';
			}
			for (var i=0,l=s_types.length; i<l; i++){
				if ($_('s-type').value == s_types[i])
					$_('b-mf'+s_types[i]).style.display = '';
				else
					$_('b-mf'+s_types[i]).style.display = 'none';
			}
		});
	}
	
	// connect to the 
	if ($('.b-leave-info').length > 0){
		$('.b-leave-info a').attr('p-href', $('.b-leave-info a').attr('href')).attr('href', 'javascript:void(0);').click(function(e){
			var popup = new oPopupLayer();
			popup.addParams({'insert_callback' : function (){post_loading(popup);}})
			popup.create();
	 		portal.ajax.load(e, {'url' : $(this).attr('p-href')}, popup.insert);
	 		});
	}
	
	 	
	$('#b-login-link').attr('href', 'javascript:void(0);').click(function(e){
		var popup = new oPopupLayer();
		popup.create();
		portal.ajax.load(e, {'url' : 'login'}, popup.insert);
	});
	
	$('#b-reg-link').attr('href', 'javascript:void(0);').click(function(e){
		var popup = new oPopupLayer();
		popup.create();
		portal.ajax.load(e, {'url' : 'register'}, popup.insert);
	});
		
	// adding listner to the calendar
	if ($('#b-cal-container').length > 0){
		var oCalendar = new ajxCalendar({'objID' : 'b-cal-container', 'ajxhref' : '/lang/'+lang_nick+'/calendar/smphr/1/ajx/1/', 'elem' : '0', 'callback': function(){}});
		if (oCalendar) oCalendar.load();
	}*/
		
	
	// checking for input hash
	if (document.location.hash){
		var result = portal.hash.parse();
 		
 		if (result['id'] || result['url']){
 			var popup = new oPopupLayer();
 			popup.create();
 			popup.addParams({'insert_callback' : function (){post_loading(popup)}});
 			
 			portal.ajax.load(null, result, popup.insert);
 		}
	} 
		
});

