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
	});

	//	do nothing on no href
	$('a[href="#"]').click(function(){ return false; }); 

	//	sets links with the rel of "blank" to open in a new window
	$('a[rel$="blank"]').attr('target', '_blank'); 
	
	//	printing articles
	$('.printing').bind('click', function(){
		window.print();
		return false;
	});
	
	//	animated scrolling for same page links
	$('a[href*=#]').click(function() {
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
			var $target = $(this.hash);
			$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
			if ($target.length) {
				var targetOffset = $target.offset().top;
				$('html,body').animate({scrollTop: targetOffset}, 1000);
				return false;
			}
		}
	});

	//	image link fade-in animation
	$('a.linkfade').each(function(){
		$('img', this).css({opacity:0.65});
		$('img', this).hover(function() {
			$(this).animate({opacity: 1}, 300);
		}, function() {
			$(this).animate({opacity: 0.65}, 300);
		});
	});
	
	//	nav image rollover effect
	if( $('.nav').length > 0 ) {
		$('.nav li').hover(
			function() { $(this).addClass('active'); $('ul', this).css('display', 'block'); },
			function() { $(this).removeClass('active'); $('ul', this).css('display', 'none'); 
		});
		
		$('.nav li').hover(function(){
			if($('a img:first', this).length > 0 )  {
				var newSrc = $('a img:first', this).attr('src').replace('_off.png','_on.png');
				$('a img:first', this).attr('src',newSrc);
			}
		},
		function(){
			if($('a img:first', this).length > 0 )  {
				var oldSrc = $('a img:first', this).attr('src').replace('_on.png','_off.png');
				$('a img:first', this).attr('src',oldSrc);
			}
		})
	}

	//	articles infinite gallery
	if( $('.gallery-carousel').length > 0 ) {
		$('.gallery-carousel').carousel('.previous', '.next');
	}
	
	//	photo Enlargement
	if( $('a[rel="colorbox"]').length > 0 ) {
		$('a[rel="colorbox"]').colorbox(
			{	slideshow:true, 
				transition:"elastic", 
				opacity: 0.75, 
				current: '{current} din {total} ',
				slideshowStart: 'porneste slideshow', 
				slideshowStop: 'opreste slideshow',
				previous: 'Precedenta',
				next: 'Urmatoarea',
				close: 'Inchide'
			}
		);
	}
	//	intro flash presentation
	if( $('#intro-flash').length > 0 ) {
		if(swfobject) {
			var flashvars = false;
			var params = {menu: "true", wmode: "transparent"};
			var attributes = false;
			
			swfobject.embedSWF('/static/flash/site/intro.swf', 'intro-flash', '900', '600', '9.0.0', '/static/flash/expressInstall.swf', flashvars, params, attributes);
		}
	}
	
	//	newsletter box
	if( $('.newsletter').length > 0 ) {
		var obj = $('.newsletter').parent();
		var opened = false;
		$('a', obj).bind('click', function(){
			if(opened) {
				$(this).next().filter('form').slideUp('slow');
				opened = false;
			} else {
				$(this).next().filter('form').slideDown('slow');
				opened = true;
			}
			return false;
		});
		$('.discard', obj).bind('click', function(){
			$(obj).find('form').slideUp('slow');
			opened = false;
			return false;
		})
	}

	//	inner photo slideshow
	if( $('.form').length > 0 ) {
		
		$('input[type="text"][name^="phone"]').mask("(+40) 999 999 999");
		$('input[type="text"][name^="birth"]').mask("99/99/9999");
		var obj = $('.form');
		var val = $('form', obj).validate({meta: "validate"});
		
		$('a.reset', obj).click(function(){
			val.resetForm();
			return false;
		})
		
		$('input[type="checkbox"][name^="others"]').each(function(){
			var next = $(this).parent().next().filter('input:first[type="text"]');
			if( $(next).length > 0 ) {
				$(this).bind('click', function(){
					if( $(this).is(':checked'))
						$(next).focus();
				})
			}
		})
		
		$('input[type="checkbox"][name^="services_"]').each(function(){
			var next = $(this).parent().next().filter('input:first[type="radio"]');
			if( $(next).length > 0 ) {
				$(this).bind('click', function(){
					if( $(this).is(':checked'))
						$(next).attr('checked', 'checked');
					else
						$(next).removeAttr('checked');
				})
			}
		})
		
		$('input[type="checkbox"][name="iagree"]').bind('click', function(){
			if( $('input[type="checkbox"][name="iagree"]').is(':checked') )
				$('input[type="submit"][name="submit"]').removeClass('disabled').val('Trimite cererea').removeAttr('disabled');
			else
				$('input[type="submit"][name="submit"]').addClass('disabled').val('Nu esti de acord cu termenii si conditiile noastre!?').attr('disabled', true);
		})

		//	slide the results box
		if( $('div.results').length  > 0 ) { 
			$('div.results').slider('up', 500, 3); 	// 4 seconds
		}
	}

	//	newsletter form
	if( $('.btns form').length > 0 ) {
		var form = $('.btns form');
		$(form).submit(function(){
			var method = $(this).attr('method');
			var url = $(this).attr('action');

			$('input[type="submit"]', form).addClass('disabled').val('Asteapta ...').attr('disabled', true);

			$.ajax({
				url:  url,
				type: method,
				data: $(form).serialize(),
				cache: false,
				timeout: 3000,
				error: function(xhr, desc, e) {
					$('input[type="submit"]', form).removeClass('disabled').val('Aboneaza-te').removeAttr('disabled');
					strError = "Unable to submit form. Please try again later.";
					console.log(strError);
					alert("The following errors were encountered:\n" + strError + "\n" + desc);
				},
				success: function(r) {
					var res = $('div.results', form);
					$(res).html(r);
					var message = $(res).find('blockquote');
					if($(message).hasClass('error'))
						$(message).slider('up', 'slow', 3, function(){
							$('input[type="submit"]', form).removeClass('disabled').val('Aboneaza-te').removeAttr('disabled');
						})
					else
						$(message).slider('up', 'slow', 3, function(){
							$(form).slideUp('slow');
							opened = false;
							$(form).find(':input, :textarea').not(':hidden').val('');
							$('input[type="submit"]', form).removeClass('disabled').val('Aboneaza-te').removeAttr('disabled');
						});
				}
			});
			return false; 
			
			$('input[type="submit"]', form).val('Asteapta ...').attr('disabled', true);
			return false;
		})
	}
	

	//	inner photo slideshow
	if( $('.slideshow').length > 0 ) {
		$('.slideshow').cycle({
			fx: 'fade' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
		});
	}
	
	
});