var lang = "ro";

var url = window.top.location.toString();
var pageHash = unescape(self.document.location.hash.substring(1));

var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
var IE7 = false /*@cc_on || @_jscript_version == 5.7 @*/;
var isMSIE = /*@cc_on!@*/false;
try {document.execCommand("BackgroundImageCache", false, true);} catch(err) {}

//This function is used to put a unique number at the end of each ajax call to prevent ie from caching it.
var nocache = function(url) {
	var rand = Math.floor(Math.random()*8999999999)+1000000000;
	url = url + (url.indexOf("?") == -1?'?nocache=':'&nocache=') + rand;
	return url;
};


$(function(){
	// css dimensions
	var w = $(window).width();
	var h = $(window).height();
		
	//	disable caching of AJAX responses
	$.ajaxSetup ({
		cache: false
	});

	//	sets links with the rel of "blank" to open in a new window
	$('a[rel$="blank"]').attr('target', '_blank'); 

	//	activating fancyzoom
	if( $('a[rel*=facebox]').length > 0 ) {
		 $('a[rel*=facebox]').facebox({ opacity : 0.8, overlay: true, loadingImage: '/static/images/facebox/loading.gif', closeImage: '/static/images/facebox/closelabel.gif'}) 
	}

	//	askus
	if( $('.askus').length > 0 ) {
		$('.askus').find('input:first').bind('click', function() { 
			var quest = $(this);
			var obj = $('.askus')
			var form = $(quest).parent();
			var url = $(form).attr('action');
			var method = $(form).attr('method');
			
			$(quest).next().filter('blockquote').slideDown('slow');
			$(quest).hide('fast');
			$(quest).next().find('a.cancel').bind('click', function(){
				$(quest).parent().find(':input, :textarea').not('input:first').not('input:last').val('');
				$(quest).next().filter('blockquote').slideUp('slow');
				$(quest).show('fast');
			})

			$('a.submit', obj).bind('click', function(){
				$(form).trigger('submit');
				return false;
			})

			$(form).bind('submit', function() {
				$('span.disabled', obj).css({'display':'inline'});
				$('a.submit', obj).css({'display':'none'});
				$.ajax({
					url:  url,
					type: method,
					data: $(form).serialize(),
					cache: false,
					timeout: 3000,
					error: function(xhr, desc, e) {
						strError = "Unable to submit form. Please try again later.";
						//console.log(strError);
						alert("The following errors were encountered:\n" + strError + "\n" + desc);
						$('span.disabled', obj).css({'display':'none'});
						$('a.submit', obj).css({'display':'inline'});
					},
					success: function(r) {
						var res = $('div.results', obj);
						$(res).html(r).css({'margin-top':'15px'});
						var message = $(res).find('blockquote');
						
						if($(message).hasClass('error')) {
							$(message).slider('up', 'slow', 3, function(){
								$('span.disabled', obj).css({'display':'none'});
								$('a.submit', obj).css({'display':'inline'});
							});
						} else {
							$(message).slider('up', 'slow', 3, function(){
								$(quest).parent().find(':input, :textarea').not('input:first').not('input:last').val('');
								$(quest).next().filter('blockquote').slideUp('slow');
								$(quest).show('fast');
								$('span.disabled', obj).css({'display':'none'});
								$('a.submit', obj).css({'display':'inline'});
							});
						}
					}
				});
				return false; 
			})

			return false;
		})
	}
	
	//	starting the header slideshow
	if( $('.slideshow').length > 0 ) {
		
		var timer = 5; // time to slide ( in seconds )
		var slide = function() {
			var obj = $('.slideshow');
		    var $active = $('img.active', obj);
		    if ( $active.length == 0 ) $active = $('img:last', obj);

		    // use this to pull the images in the order they appear in the markup
		    var $next =  $active.next().length ? $active.next() : $('img:first', obj);

		    // uncomment the 3 lines below to pull the images in random order
			// var $sibs  = $active.siblings();
			// var rndNum = Math.floor(Math.random() * $sibs.length );
			// var $next  = $( $sibs[ rndNum ] );
		    $active.addClass('last-active');
			$next.css({opacity: 0.0})
				.addClass('active')
				.animate({opacity: 1.0}, 1000, function() {
					$active.removeClass('active last-active');
				});
		}
	    setInterval(slide, timer*1000);
	}
	
	//	printing articles
	$('.printing').bind('click', function(){
		window.print();
		return false;
	});
	
});