
$(function(){
	$('a[href=#], .prevent').live('click', function(e) { e.preventDefault(); });
	
	$.ajaxSetup({
		error: function(x, t, e){
			/*if ( x.responseText !== '' ) {
				$.pnotify({
					pnotify_title: 'Error',
					pnotify_text: x.responseText,
					pnotify_type: 'error'
				});
			}*/
		}
	});
});

/*$(function(){
	$('a').live('click', function(e){
		
		// Internal links only
		if ( $(this).attr('href')[0] == '/' ) {
			e.preventDefault();
			
			$.ajax({
				url : $(this).attr('href'),
				context : $(this),
				dataType : "html",
				type : "get",
				data : { ajax : 1 },
				beforeSend : function(){
					//$(this).next().show();
				},
				success : function(re){
					var s = re.split('!!WTFMF!!');
					var p = $.evalJSON(s[0]);
					
					$('div.tcontent').html(s[1]);
					$('title').html(p.title);
					
					// Attach the css files
					if ( p.scripts.css.length > 0 ) {
						for ( var i in p.scripts.css ) {
							if ( $('link[href="' + p.scripts.css[i] + '"]').length == 0 ) {
								$('<link />', {
									href : p.scripts.css[i],
									media : 'all',
									rel : 'stylesheet'
								}).appendTo('head');
							}
						}
					}
					
					// Attach the js files
					if ( p.scripts.js.length > 0 ) {
						for ( var i in p.scripts.js ) {
							if ( $('script[src="' + p.scripts.js[i] + '"]').length == 0 ) {
								loadjscssfile(p.scripts.js[i], "js")
							}
						}
					}
				}
			});
		}
		
	});
});*/

$.preload = function() {
	for ( var i = 0; i < arguments.length; i++ ) {
		$("<img />").attr("src", arguments[i]);
	}
}

$(function(){
	
	$desc = $('div.fp-de');
	
	$desc
		.data({
			'che' : $desc.css('height'),
			'ohe' : $desc.css('height', 'auto').height() 
		})
		.css('height', $desc.data('che'));
	
	// Show/hide descr
	$('div.show-descr a')
		.hover(function(){
			$span = $(this).find('span');
			$span.toggleClass( $span.hasClass('b-exp') ? 'b-exp-hov' : 'b-hide-hov' );
		}, function(){
			$span = $(this).find('span');
			$span.removeClass( $span.hasClass('b-exp') ? 'b-exp-hov' : 'b-hide-hov' );
		})
		.click(function(){
			$span = $(this).find('span');
			var nHeight = $span.hasClass('b-exp') ? $('div.fp-de').data('ohe') : $('div.fp-de').data('che');
			
			$span
				.toggleClass('b-exp b-hide b-exp-hov b-hide-hov')
				.prevAll()
					.toggleClass('none')
				.parents('div.fp-descr')
					.find('div.fp-de')
						.animate({ 'height' : nHeight }, 'fast');
		});
	
	// Preload the images
	$('img.preload').each(function(){
		$.preload($(this).attr('src'));
	});
	
	$('.tsMakeOpinion')
		.click(function(){
			var $q = $(this).parents('form').find(':checked');
			
			if ( $q.length == 0 ) { return; }
			
			$.ajax({
				url : $(this).parents('form').attr('action'),
				context : $(this),
				dataType: "json",
				type: "post",
				data : {
					id : $(this).attr('rel'),					
					opt : $q.val()			
				},
				beforeSend : function(){
					$(this).next().show();
				},
				success : function(re){
					$(this).next().hide().next().show();
					
					$('input.tsMakeOpinion').hide();
					
					$('div.tsoOpts div.ts-opt')
						.each(function(){
							var a = re[$(this).attr('rel')];
							
							$(this)
								.addClass('result')
								.find('input')
									.hide()
									.next()
										.addClass('block result')
										.next()
											.show()
											.find('div.tso-bar')
												.css('width', a.perc + '%')
												.find('span')
													.html(a.text);
						});
				}
			});
			
		}); 
	
});
