jQuery.fn.ImageRotator = function() {

	var obj = $(this);
	var curr = 0;
	var length = $(obj).find('img').length;

	if (length == 1) return;
	
	$(obj).find('img').css({position: 'absolute', display: 'none', top: '0px', right: '0px'});
	$(obj).find('img:eq(0)').css({display: 'block'});
	$(obj).css({position: 'relative'});

	var inter = setInterval(function(){

		if ($(obj).find('img:eq(' + curr + ')').length == 1)
		{
			$(obj).find('img:eq(' + curr + ')').fadeOut();
			curr++;
			if (curr == length) curr = 0;
			
			$(obj).find('img:eq(' + curr + ')').fadeIn();
		}
		else clearInterval(inter);
	}, 4000);

};

jQuery.fn.bubble = function(text) {

	$(this).each(function(){
		
		var obj = $(this);
		var position = $(this).position();
	
		var newDate = new Date;
		var id = newDate.getTime();
		
		position.top -= 94;
	
	
		$(this).after('<div id="bubble_' + id + '" class="bubble" style="left:' + position.left + 'px; top:' + position.top + 'px;">' + text + '</div>');
		
		$(this).hover(function(){ $('#bubble_' + id).show(); }, function(){ $('#bubble_' + id).stop().hide(); });
	});
};


jQuery.fn.bubbleWin = function() {

	$(this).each(function(){
		var name = $(this).attr('name');
		var win = $('.container[name=' + name + ']');

		$(win).css({position: 'absolute', zIndex: 10000});
		$(win).prepend('<div class="bubbleArrow top"></div>').hide();
		if (!$(win).hasClass('noOpacity')) $(win).css('opacity', 0.8);
		
		$(this).click(function(e){
		
			ret = false;
			if ($(win).is(':visible')) ret = true;
			
			$('.bubbleWin.selected').each(function(){
							
				$(this).removeClass('selected');
				$('.container[name=' + $(this).attr('name') + ']').hide();
				return;
			});
			
			if (ret) return;
			
			
			$(this).addClass('selected');
			
			$(win).show();
			pos = $(win).offset();
			pos2 = $(this).offset();

			if ((pos.top - $(this).width()) > pos2.top)
			{
				plc = ($(this).width() / 2) - (25 / 2);
				$('.bubbleArrow', win).addClass('top').offset({left: (pos2.left + plc)});
			}
			else if ((pos.top + $(win).height()) < pos2.top)
			{
				plc = ($(this).width() / 2) - (25 / 2);
				$('.bubbleArrow', win).addClass('bot').offset({left: (pos2.left + plc)});
			}
			else if (pos.left > pos2.left)
			{
				plc = ($(this).height() / 2) - (25 / 2);
				$('.bubbleArrow', win).addClass('l').offset({top: (pos2.top + plc)});
			}
			else
			{
				plc = ($(this).height() / 2) - (25 / 2);
				$('.bubbleArrow', win).addClass('r').offset({top:(pos2.top + plc)});
			}
			
		});
	});
};

function CloseBubble()
{
	$('.bubbleWin.selected').each(function(){
							
		$(this).removeClass('selected');
		$('.container[name=' + $(this).attr('name') + ']').hide();
		return;
	});
}


$(document).ready(function(){
		
	/* UI */
	$('.radio').each(function(){
		var value = '';
		var sel = $('.selected', this);
		if (sel.length > 0) value = $(sel).attr('value');
		$(this).after('<input type="hidden" name="' + $(this).attr('name') + '" value="' + value + '" />');
	});
	
	$('.radio a').live('click', function(){
		var InpName = $(this).closest('.radio').attr('name');
		$(this).closest('.radio').find('a').removeClass('selected');
		$(this).addClass('selected');
		
		$('input[name=' + InpName + ']').val($(this).attr('value')).change();
	});
	
	
	$.ajaxSetup({ cache: false });

	
	var $_GET = [];

	document.location.search.replace(/\??(?:([^=]+)=([^&]*)&?)/g, function () {
		function decode(s) {
			return decodeURIComponent(s.split("+").join(" "));
		}
	
		$_GET[decode(arguments[1])] = decode(arguments[2]);
	});
	
	$('a[rel=blank]').attr('target', '_blank');
	
	$('.Nadlan .search select[name=area]').change(function(){
		$('.Nadlan .search select[name=city]').html('');
		if ($(this).val() != "")
		{
			$.get("index.php?module=Nadlan&pname=getCities&pmode=empg","id=" + $(this).val(),function(msg){
																											   
				$('#ajaxWin').append(msg); //alert(msg);	
			});
		}
	});
	
	/* Home Page */
	InitPage($('#page'));
	
	$(".youtube").click(function() {
		$.fancybox({
			'padding'		: 0,
			'autoScale'		: false,
			'transitionIn'	: 'none',
			'transitionOut'	: 'none',
			'title'			: this.title,
			'width'			: 680,
			'height'		: 495,
			'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/') + "&autoplay=1&rel=0",
			'type'			: 'swf',
			'swf'			: {
			    'wmode'				: 'transparent',
				'allowfullscreen'	: 'true'
			}
		});

		return false;
	});
});


function twitterShare() {
    window.open('http://twitter.com?status=' + encodeURIComponent(document.title + ' - ' + document.location));
}
function facebookShare() {
    var d = document,
        f = 'http://www.facebook.com/share',
        p = '.php?src=bm&v=4&i=1259575049&u=' +
            encodeURIComponent(document.location.href) +
            '&t=' + encodeURIComponent(d.title);
    function a() {
        if (!window.open(f + 'r' + p, 'sharer', 'toolbar=0,status=0,resizable=1,width=626,height=436')) {
            document.location.href = f + p;
        }
    }
    if (/Firefox/.test(navigator.userAgent)) {
        setTimeout(a, 0);
    } else {
        a();
    }
}





jQuery.fn.ULPaging = function(maxInPage) {

	var obj = this;
	var appI = 0;
	var appUls = '';
	var modu;
	var lastPage = 0;
	
	$(this).addClass('ULPaging');
	
	$('.ul .strip', this).each(function(){
		appI++;
		modu = (appI % maxInPage);
		if (modu == 1) appUls += '<div class="ul">';
		
		appUls += $(this).outerHTML();
		
		if (modu == 0) appUls += '</div>';
	});
	
	if (modu != 0) appUls += '</div>';

	$(this).html('<div class="wrap">' + appUls + '</div>');
	$('.ul', this).hide();
	$('.ul:first-child', this).show();
	
	var appUlsNUM = $('.ul', this).length;
	var appUlsMENU = '<div class="menu">';
	for (i=1; i<=appUlsNUM; i++) appUlsMENU += '<a num="' + i + '"></a>';
	appUlsMENU += '</div>';
	
	appUlsMENU += '<a class="arr rightArr"></a><a class="arr leftArr"></a>';
	
	$(this).append(appUlsMENU);
	
	// Height & Width
	var winWidth = $(this).width();
	$('.wrap', this).css('height', $('.ul', this).height());
	$('.ul', this).css('width', winWidth);
	
	// Move Page
	$('.menu a', this).click(function(){
		
		if ($(this).hasClass('selected')) return;
		
		var num = $(this).attr('num');
		num--;
		
		winWidthMove = 1;
		if (lastPage > num) winWidthMove = -1;
		
		if ($('.ul:visible', obj).is(':animated') || $('.ul:eq(' + num + ')', obj).is(':animated')) return;
		
		if ($('#center').hasClass('lang_1'))
		{
			$('.ul:visible', obj).animate({right:(20 * winWidthMove)}, 100, 'swing');
			
			$('.ul:visible', obj).animate({right:-(winWidth * winWidthMove), opacity: 0.25}, 400, 'swing', function(){
				$(this).hide().css({right:'0px'});
			});
			
			$('.ul:eq(' + num + ')', obj).css({right:(winWidth * winWidthMove), opacity: 0}).animate({right:((winWidth + 20) * winWidthMove)}, 100, 'swing');
			
			$('.ul:eq(' + num + ')', obj).show().animate({right:0, opacity: 1}, 400, 'swing');
		}
		else
		{
			$('.ul:visible', obj).animate({left:(20 * winWidthMove)}, 100, 'swing');
			
			$('.ul:visible', obj).animate({left:-(winWidth * winWidthMove), opacity: 0.25}, 400, 'swing', function(){
				$(this).hide().css({left:'0px'});
			});
			
			$('.ul:eq(' + num + ')', obj).css({left:(winWidth * winWidthMove), opacity: 0}).animate({left:((winWidth + 20) * winWidthMove)}, 100, 'swing');
			
			$('.ul:eq(' + num + ')', obj).show().animate({left:0, opacity: 1}, 400, 'swing');
		}
		
		$('.menu a', obj).removeClass('selected');
		$(this).addClass('selected');
		
		lastPage = num;
	});
	$('.menu a:eq(0)').addClass('selected');
	
	
	$(this).bind('mousewheel', function(event,delta){
		
		 if (delta > 0) {
		   // mousewheel is going up; 
		   $('.menu a.selected', this).next().click();
		 } else {
		   // mousewheel is going down 
		   $('.menu a.selected', this).prev().click();
		 }
		 
		 return false;
	});
	
	
	$('a.arr', this).click(function(){
		if ($(this).hasClass('rightArr')) $('.menu a.selected', obj).next().click();
		else $('.menu a.selected', obj).prev().click();
		
		return false;
	});
};


function number_format (number, decimals, dec_point, thousands_sep) {

    number = (number + '').replace(/[^0-9+\-Ee.]/g, '');
    var n = !isFinite(+number) ? 0 : +number,
        prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),        sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
        dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
        s = '',
        toFixedFix = function (n, prec) {
            var k = Math.pow(10, prec);            return '' + Math.round(n * k) / k;
        };
    // Fix for IE parseFloat(0.55).toFixed(0) = 0;
    s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
    if (s[0].length > 3) {        s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
    }
    if ((s[1] || '').length < prec) {
        s[1] = s[1] || '';
        s[1] += new Array(prec - s[1].length + 1).join('0');    }
    return s.join(dec);
}


function InitPage(obj)
{
	
	//$("SELECT.stylish", obj).selectBox();
	
	$('.button.btn', obj).click(
		function(){ 
			if ($(this).hasClass('selected')) $(this).addClass('selected');
			else $(this).removeClass('selected');
		}
	)
	
	
	$('.fancy', obj).MyFancy();
	
	$('.logged', obj).click(function(){
		
		if (arrSettings['username'] == '')
		{
			$.MyFancy('Signup',null, 'Signup');
			return false;
		}
	});

	// BubbleWins
	$('.bubbleWin', obj).bubbleWin();
	
	
	/* Changeable Input */
	$('.changable', obj).removeClass('changable').closest('li').addClass('changable');
	$('.changable:not(.password) input[value=""]', obj).parent().removeClass('changable');
	$('.changable input', obj).attr('readonly', true).after('<a class="change">Change</a>');
	
	$('.changable .change', obj).live('click', function(){
		$(this).closest('.changable').find('input').removeAttr("readonly");
		$(this).closest('.changable').removeClass('changable');
		$(this).remove();
	});
}

