$(document).ready(function(){ 
	if($(window).width() <= 1024) {
		$('div.skyscrapper_side').remove();
	} else {
		$('.skyscrapper_side').css('width', '120px');
		$('div.holder_site_and_banner').css('width', '1244px');
	}
});

try { document.execCommand("BackgroundImageCache", false, true);} catch(err) {}
var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
var IE7 = false /*@cc_on || @_jscript_version == 5.7 @*/;
var isMSIE = /*@cc_on!@*/false;
var initValue;
var swfu;
function disable_rating(el){
	$(el).removeClass("rating");
	$(el).addClass("rated");
}

function set_rating(el,rate){
	$(el).css("width","50px");
	$(el).children(".current-rating").html("Currently "+rate.rating+"/5 Stars.");
	$(el).children(".current-rating").css("width",(rate.rating*20)+"%");
	$(el).next("span").html(rate.rating+" ("+rate.count+" voturi)");
}
var field_reset = function(el){
	var changed = false;
	var init_val = "";
	el.focus(function(){
		if(changed == false){
			init_val = $(this).val();
			$(this).val("");
			changed = true;
		}
	});
	el.blur(function(){
		if($(this).val() == ""){
			$(this).val(init_val);
			changed = false;
		}
	});
}
$(function() {
	field_reset($('input[name=keyword]'));
	$(".rating a").click(function(){
		var mark = $(this).html();
		var parent_ul = $(this).parent("li").parent("ul");
		var item_id = $(parent_ul).attr("id").replace("item_","");
		var rating_url = baseURL+"items/rate_item";
		$.post(rating_url,{rating:mark,item:item_id},function(data){
			if(data == ""){
				alert("Ne pare rau insa nu puteti vota decat o singura data pe zi.");
				return false;
			} else {
				var  result = eval("("+data+")");
			}
			set_rating(parent_ul,result);
			disable_rating(parent_ul);
		})
	});
//	$('.dummyRating').ntzRateThis();

	$('#listareAnunturi table td').click(function(){
		window.location=$(this).parent().find('td a').attr('href');
	})
//	$('#searchSummary select').change(function(){
//		window.location=$(this).val()
//	})
	$('.nicePagination dt').click(function(){
		if($(this).hasClass('open')){
			$(this).removeClass('open').next().stop().fadeOut(150, function(){$(this).removeAttr('style');});
		} else {
			$(this).addClass('open').next().stop().fadeIn(150, function(){
				$('body').one('click', function(){
					$('.nicePagination dt').removeClass('open').next().stop().fadeOut(150, function(){$(this).removeAttr('style');});
				})
			});

		}
	});


	$('dl.relatedDownloads dd').click(function(){
		window.location= $(this).find('a.download_title').attr('href');
	})
	$('div.relatedClassifieds ul li').click(function(){
		window.location = $(this).find('a.more').attr('href');
	});
	$('dl.descopera dd').each(function(k,v){
		if ($(v).hasClass("same")){
			return;
		}
		$(v).unbind("click");
		$(v).click(function(){
			window.open($(this).find('a.download_title').attr('href'),"descopera_window");
		});
	})
	$('.featured dd').each(function(){
		var _this = this;
		$(_this).find('a').attr('href', $(_this).find('h2 a').attr('href'));
		$(_this).unbind().click(function(){
			window.location=$(_this).find('a').attr('href');
		})
	})
	

	$('#searchSummary select, #detaliiCont select, #addAnunt select, #stiriArticole form.search select, #download_form select, #filterSearch select').selectbox();
		//Punem clasa hover pe elementele necesare
		$('#comparator2 dd, #comparator dd, .produseFeatured li, dl.relatedDownloads dd, .produsePopulare li, dl.featured dd, .relatedDownloads li, ul#main-menu li, ul#account-menu li, #fresh-news ul li, .downloads ul li, .ads ul li, .populars ul li, ul.titles li, span.read-more, div.recommendation').hover(
				function() {$(this).addClass('hover');},
				function() {$(this).removeClass('hover');}
		);
		$('ul.stars li').hover(
				function() {
						$(this).addClass('hover');
						$(this).prevAll().addClass('hover');
				},
				function() {
						$(this).removeClass('hover');
						$(this).prevAll().removeClass('hover');
				}				
		);

		//Slider pe meniul secundar
		$('ul#secondary-menu li ul').each(function(){ //setam un tragaci
				var trigger = $(this).parents('li').children('a')
				trigger.addClass('trigger');
		});
		$('#secondary-menu .trigger').click(function(){ //punem interactiunea pe el
				var fatherLi = $(this).parents('li');
				if ( fatherLi.hasClass('active') ) {
						fatherLi.children('ul').slideUp();
						fatherLi.removeClass('active');
				} else {
						fatherLi.children('ul').slideDown();
						fatherLi.addClass('active');
				};
		});

		
		$('ul.titles li, .ads ul li, .recommendation').click(function() {
				var URL = $(this).find('a').attr('href');
				window.location = URL;
		});
		$('.populars ul li .thumb').click(function() {
				var URL = $(this).parents('li').children('h3').children('a').attr('href');
				window.location = URL;
		});
		
		
		//Formulare
		$(':text').each(function(){
			$(this).focus(function(){
				initValue = $(this).attr('value');
				$(this).select().addClass('focus');
			});
			$(this).blur(function(){
				if($(this).val() == ''){
					$(this).val(initValue);
				}
				$(this).removeClass('focus');
			});
		});

		
/* -----------------------------------------------------------------------------------------  */ 
$('.producatori a.call-to-action').unbind().click(function(){
	var _this = this;
	if($('.producatori ul.totiProducatorii').is(':visible')) {
		$('.producatori ul.totiProducatorii').slideUp();
		$(_this).find('span').html('Toţi producătorii &raquo;');
	} else {
		$('.producatori ul.totiProducatorii').slideDown();
		$(_this).find('span').html('Mai puţini producători &laquo;');
	}
	return false;
});
/* -----------------------------------------------------------------------------------------  */ 
	$('.accordion dt, .accordion dd').hover(function(){$(this).addClass('over')}, function(){$(this).removeClass('over')});
	$(".accordion").accordion({
		active: '.selected',
		selectedClass: 'open',
    	header: "dt"
	});
	$('.accordion').each(function(){
		var _this=this;
		$(_this).find('dt').each(function(){
			var t=this;
			var url = $(t).next("dd").find('a[href^=http]').attr('href');
			if(url == undefined){
				 url = $(t).find('a[href^=http]').attr('href');
			}
			$(t).next('dd').click(function(){
				window.location = url;
				return false;
			});
			$(t).next('dd').find('.more a').attr('href', url)
		})
	});

		
});

function categoryPage() {
	if(IE6){
		$('.anunturiBox li').hover(function(){$(this).addClass('over')}, function(){$(this).removeClass('over')});
	}
};

function productPage() {
	if(IE6){
		$('.relatedClassifieds li').hover(function(){$(this).addClass('over')}, function(){$(this).removeClass('over')});
	}
	relatedClassifieds();
};

function relatedClassifieds() {
	$('.relatedClassifieds li').click(function(){
		window.location=$(this).find('a').attr('href');
	});
	$('.more').click(function(){
		window.location=$(this).parent().find('a').attr('href');
	});
};

function comparator() {
	try{$('#comparator span.dummy').droppable( "destroy" );}catch(err){}
	try{$('#comparator2 .produse dd a.bigThumb').draggable( "destroy" )}catch(err){}
	$('#comparator2 .produse dd a.bigThumb').css({cursor:'pointer'})	
	$('#comparator dd').each(function(){
		var _this=this;
		$(this).find('a.del').unbind().click(function(){
			$(_this).find('div').fadeOut(100, function(){
				$(this).remove();
				$(_this).addClass('empty').find('.dummy').fadeIn(function(){
					comparator()
				});
				
			});			
			return false;
		});
	});
	$('#comparator2 .produse dd a.bigThumb').unbind().click(function(){$(this).prev().trigger('click');return false;});
	$('#comparator2 .produse dd a.comp').unbind().click(function(){
		var t=this;
		if($('#comparator span.dummy:visible').length>0){
			$('#comparator2 .produse dd a.comp').unbind().click(function(){return false;})
			$(this).next().effect("transfer", { to: "#comparator span.dummy:visible:eq(0)" }, 400, function(e){
				$('#comparator span.dummy:visible:eq(0)').addClass('loading');
				$.ajax({
					type: "GET",
					url: $(t).attr('href'),
					cache:	false,
					success: function(data){
						$('#comparator span.dummy:visible:eq(0)').removeClass('loading').fadeOut(100, function(){
							$(this).parent().removeClass('empty').append($(data).find('div')).fadeIn(function(){
								comparator();
							});
						});
					}
				});
			});
		} 
		if($('#comparator span.dummy:visible').length==0) {
			alert('Se pot compara maxim cinci produse')
		}
		

		return false;
	});
	
if($('#comparator span.dummy:visible').length>0){
	$('#comparator2 .produse dd a.bigThumb').css({cursor:'move'})
	/* ----------------------------------------------------------------------------------------- let's drag some stuff! */ 
	$('#comparator2 .produse dd a.bigThumb').draggable({
		opacity:	0.8,
		helper:		'clone',
		revert:		true,
		scroll:		true,
		scrollSpeed:	10,
		delay:	150,
		distance: 5
	});
	
	/* ----------------------------------------------------------------------------------------- let's drop some stuff! */ 
	$('#comparator span.dummy:visible').droppable({
		accept: ".bigThumb",
		activeClass: 'droppable-active',
		hoverClass: 'droppable-hover',
		drop: function(e,ui) {
			var t=this;
			$(t).addClass('loading');
				$.ajax({
					type: "GET",
					url: $(ui.draggable).attr('href'),
					cache:	false,
					success: function(data){
						$(t).removeClass('loading').fadeOut(150, function(){
							$(t).parent().removeClass('empty').append($(data).find('div')).fadeIn(function(){
								comparator();
							});
						});
					}
				});
		}
	});
}
};



function carusel() {
	$('#caruselGalerie ul').width(($('#caruselGalerie li').length*190)+40);
	$('#caruselGalerie .prev').click(function(){
		$('#caruselGalerie .slider').scrollTo( '-=699', 500, {axis:'x'} );
		return false;
	});
	$('#caruselGalerie .next').click(function(){
		$('#caruselGalerie .slider').scrollTo( '+=699', 500, {axis:'x'} );
		return false;
	});

};

/* -----------------------------------------------------------------------------------------  */ 
/* -----------------------------------------------------------------------------------------  */ 
/* -----------------------------------------------------------------------------------------  */ 
/* -----------------------------------------------------------------------------------------  */ 







(function( $ ){
	var $scrollTo = $.scrollTo = function( target, duration, settings ){
		$scrollTo.window().scrollTo( target, duration, settings );
	};

	$scrollTo.defaults = {
		axis:'y',
		duration:1
	};

	//returns the element that needs to be animated to scroll the window
	$scrollTo.window = function(){
		return $( $.browser.safari ? 'body' : 'html' );
	};

	$.fn.scrollTo = function( target, duration, settings ){
		if( typeof duration == 'object' ){
			settings = duration;
			duration = 0;
		}
		settings = $.extend( {}, $scrollTo.defaults, settings );
		duration = duration || settings.speed || settings.duration;//speed is still recognized for backwards compatibility
		settings.queue = settings.queue && settings.axis.length > 1;//make sure the settings are given right
		if( settings.queue )
			duration /= 2;//let's keep the overall speed, the same.
		settings.offset = both( settings.offset );
		settings.over = both( settings.over );

		return this.each(function(){
			var elem = this, $elem = $(elem),
				t = target, toff, attr = {},
				win = $elem.is('html,body');
			switch( typeof t ){
				case 'number'://will pass the regex
				case 'string':
					if( /^([+-]=)?\d+(px)?$/.test(t) ){
						t = both( t );
						break;//we are done
					}
					t = $(t,this);// relative selector, no break!
				case 'object':
					if( t.is || t.style )//DOM/jQuery
						toff = (t = $(t)).offset();//get the real position of the target 
			}
			$.each( settings.axis.split(''), function( i, axis ){
				var Pos	= axis == 'x' ? 'Left' : 'Top',
					pos = Pos.toLowerCase(),
					key = 'scroll' + Pos,
					act = elem[key],
					Dim = axis == 'x' ? 'Width' : 'Height',
					dim = Dim.toLowerCase();

				if( toff ){//jQuery/DOM
					attr[key] = toff[pos] + ( win ? 0 : act - $elem.offset()[pos] );

					if( settings.margin ){//if it's a dom element, reduce the margin
						attr[key] -= parseInt(t.css('margin'+Pos)) || 0;
						attr[key] -= parseInt(t.css('border'+Pos+'Width')) || 0;
					}
					
					attr[key] += settings.offset[pos] || 0;//add/deduct the offset
					
					if( settings.over[pos] )//scroll to a fraction of its width/height
						attr[key] += t[dim]() * settings.over[pos];
				}else
					attr[key] = t[pos];//remove the unnecesary 'px'

				if( /^\d+$/.test(attr[key]) )//number or 'number'
					attr[key] = attr[key] <= 0 ? 0 : Math.min( attr[key], max(Dim) );//check the limits

				if( !i && settings.queue ){//queueing each axis is required					
					if( act != attr[key] )//don't waste time animating, if there's no need.
						animate( settings.onAfterFirst );//intermediate animation
					delete attr[key];//don't animate this axis again in the next iteration.
				}
			});			
			animate( settings.onAfter );			

			function animate( callback ){
				$elem.animate( attr, duration, settings.easing, callback && function(){
					callback.call(this, target);
				});
			};
			function max( Dim ){
				var el = win ? $.browser.opera ? document.body : document.documentElement : elem;
				return el['scroll'+Dim] - el['client'+Dim];
			};
		});
	};

	function both( val ){
		return typeof val == 'object' ? val : { top:val, left:val };
	};

})( jQuery );




(function($){
	$.fn.ntzRateThis = function(options) {
	$(this).each(function(){
		
		var t = this;
		var elHref = $(t).attr('title');
		var	notaCurenta = parseInt($(t).find('span').css('width'));
		var star = '<span class="ntzRating"><u class="notaCurenta" style="width:'+notaCurenta+'%"></u>';
     star += '<i class="one">1</i>';
     star += '<i class="two">2</i>';
     star += '<i class="three">3</i>';
     star += '<i class="four">4</i>';
     star += '<i class="five">5</i></span>';

		$(t).removeAttr('title').addClass('replaced').empty().append(star);
		$(t).find('i').hover(function(){$(this).addClass('hover')}, function(){$(this).removeClass('hover')}).click(function(){
			var _this = this;
			$(_this).parents('.ntzRating').find('i').unbind().click(function(){
				return false;
			});
					
			
			$.ajax({
				type: 'GET',
				url: elHref+'&nota='+($(t).find('i').index(_this)+1),
				success:	function(newRate){
					$(_this).parents('.ntzRating').find('.notaCurenta').css({width:newRate+'%'})
				}
			});
			return false;
		});
		return;
	});//each
};
})(jQuery);

/* -----------------------------------------------------------------------------------------  */ 
/* -----------------------------------------------------------------------------------------  */ 
/* -----------------------------------------------------------------------------------------  */ 
/* -----------------------------------------------------------------------------------------  */ 


String.prototype.replaceAll = function(strTarget, strSubString){
	var strText = this;
	var intIndexOfMatch = strText.indexOf( strTarget );
		while (intIndexOfMatch != -1){
 		strText = strText.replace( strTarget, strSubString )
		intIndexOfMatch = strText.indexOf( strTarget );
 	};
		return( strText );
};

String.prototype.ucFirst = function () {
	return this.substr(0,1).toUpperCase() + this.substr(1,this.length);
};

function subscribeToEntityAlerts (entityId) {
	var queryData = {'entity_id' : entityId};
	var subscriptionData = null;

	$.ajax({
		url : baseURL + 'newsletters/ajax_subscribe_entity_alerts',
		type : 'post',
		async : false,
		dataType : 'json',
		data : queryData,
		success: function (data) {
			subscriptionData = data;
		}
	});

	if (subscriptionData != null && subscriptionData.message != undefined) {
		alert(subscriptionData.message);
		if (subscriptionData.type != 'error') {
			location.reload();
		}
	}
}
$().ready(function () {
	$('#abort_ad_publish').click(abort_publish);
});
function abort_publish(event) {
	var abort = confirm('Esti sigur ca vrei sa parasesti pagina fara sa publici anuntul in curs de editare?');
	if (abort == true) {
		return true;
	} else {
		event.preventDefault();
		return false;
	}
}
