/***
* @author 			Oskar Hasinski ( hasinski@popsit.com )
* @package			ItemtManager
* 
* @created			20.12.2008
* @updated			07.02.2009
* @updateAuthor		Oskar Hasinski
* 
* @copy				Oskar Hasinski, Miet24 GmbH
**/

//String.ucwords( $str )
//{
//	return ($str + '').replace(/^([a-z])|\s+([a-z])/g, function ($1) {
//        return $1.toUpperCase();
//    });
//}

String.prototype.ucwords = function(){
	return this.replace(/\w+/g, function(a){
        return a.charAt(0).toUpperCase() + a.substr(1).toLowerCase();
    });
};


/* jQuery expand */
jQuery.preloadImages = function()
{
	for(var i = 0; i<arguments.length; i++)
	{
		jQuery("<img>").attr("src", arguments[i]);
	};
};





jQuery.implode = function( glue,pieces )
{
	return ( ( pieces instanceof Array ) ? pieces.join( glue ) : pieces );

};
jQuery.in_array = function( needle,haystack,argStrict )
{
	var key = '', strict = !!argStrict;
 
	if (strict)
	{
		for (key in haystack)
		{
			if (haystack[key] === needle)
			{
				return true;
			};
		};
	}
	else
	{
		for (key in haystack)
		{
			if (haystack[key] == needle)
			{
				return true;
			};
		};
	};
 
    return false;
};

jQuery.disableKey	= function( $key, $scope )
{
	var $key_code;
	$( $scope ).bind( 'keydown', function( event ){
		if( window.event )
		{
			$key_code	= window.event.keyCode;
		}
		else
		{
			$key_code	= event.which;
		}
		
		if( $key_code == $key )
		{
			event.preventDefault();
		}
	});	
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

jQuery.getUrlVars = function()
{
	var vars = [], hash;
	var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
	for(var i = 0; i < hashes.length; i++)
	{
		hash = hashes[i].split('=');
		vars.push(hash[0]);
		vars[hash[0]] = hash[1];
	};
	return vars;
};

jQuery.getUrlVar = function( name )
{
	return $.getUrlVars()[name];
};

/**
 * SET class
 * @author 		Oskar Hasinski (hasinski@popsit.com)
 * @created		01.02.2009
 * @updated		07.02.2009
 **/

function _set()
{
	this.data_arr 			= Array();
	this.scope 				= '';
	this.valStatus		= Array();
	
	this.data = function( key,value )
	{
		this.data_arr[key] = value;
	};
	
	this.setScope = function (scope)
	{
		this.scope = '#'+scope+' ';
	};
}
set = new _set();

_set.prototype.loadingMask = function( haystack,customize,ie6strict )
{
	var rs = document.createElement('div');
	rs.id 						= 'request_shutter';
	rs.style.width				= ($( haystack ).width()-customize[0])+'px';
	rs.style.height				= ($( haystack ).height()-customize[1])+'px';
	rs.style.backgroundColor	= '#fff';
	rs.style.position			= 'absolute';
	rs.style.top				= customize[2]+'px';
	rs.style.left				= customize[3]+'px';
	rs.style.zIndex				= '99';
	rs.style.opacity			= '0.'+customize[4];
	rs.style.filter				= 'alpha(opacity='+customize[4]+'0)';
	
	var ri = document.createElement('img');
	ri.src 						= '/skin/nekretnine/images/icons/load.gif';
	ri.style.top				= ( ( $( haystack ).height()-customize[1]-20)/2 )+'px';
	ri.style.left				= ( ( $( haystack ).width()-customize[0]-2)/2 )+'px';
	ri.style.zIndex				= '299';
	ri.style.height				= '20px';
	ri.style.width				= '20px';
	ri.style.filter				= 'alpha(opacity=100)';
	ri.style.position			= 'absolute';
	
	_set.prototype.request_loading_div = rs;
	
	if( ie6strict && $.browser.msie && $.browser.version < 7 )
	{
		$( rs ).append('<iframe></iframe>');
	}
	$( rs ).append( ri );
	
	$( haystack ).css({'position':'relative'});
	$( haystack ).append( rs );
};

_set.prototype.removeLoadingMask = function()
{
	$( set.request_loading_div ).remove();
};

/**
 * GET class
 * @author 		Oskar Hasinski (hasinski@popsit.com)
 * @created		01.02.2009
 * @updated		06.02.2009
 **/

function _get()
{
	this.data = function ( key )
	{
		return set.data_arr[key];
	};
	
	this.date = function()
	{
		return new Date;
	};
	
	this.charactersPosition = function(pattern, needle)
	{
		var position = Array();
		var arr = 0;
		for(i=0; i<pattern.length; i++)
		{
			if(pattern.charAt(i) == needle)
			{
				position[arr] = i;
				arr++;
			};
		};	
		return arr[0];
	};
};
get = new _get();

_get.prototype.position = function( obj )
{
	var $left	= 0; 
	var $top	= 0;
	if (obj.offsetParent) {
		do {
			$left	+= obj.offsetLeft;
			$top	+= obj.offsetTop;
		} while (obj = obj.offsetParent);
		
		return [$left,$top];
	};
};



/*
 * Startup
 */
$(document).ready(function()
{
	
	$("select#country").change(function()
	{
		var code = $(this).val();
		var path = "/globalfiles/js/regions/"+code+".js";
		
		$.getScript(path, function()
		{
      		var options = '';

			for (var i = 0; i < states[code].length; i++) 
      		{
				//alert(states[code][i]["name"]+' '+states[code][i]["state_id"]);
        		options += '<option value="' + states[code][i]["state_id"] + '">' + states[code][i]["name"] + '</option>';
      		};
      		$("select#state").html(options);
      		
		});
  	});
	
	$(".dropdown dt span").click(function() {
        $(".dropdown dd ul").toggle();
    });
                
	$(".dropdown dd ul li a").click(function() {
	        $(".dropdown dd ul").hide();
	});

	if( typeof( set.tabbed ) !== 'undefined' ) set.tabbed();
	if( typeof( set.dropped ) !== 'undefined' ) set.dropped();
	
	setTimeout( 'tmpSlider()', 500 );
	
//	$('#banner_1').flash({swf:'/skin/nekretnine/images/banner/2010/11/henkel_320x170.swf',width:320,height:170 });
//	$('#banner_2').flash({swf:'/skin/nekretnine/images/banner/2010/11/ytong.swf',width:320,height:232 });
//	$('#banner_3').flash({swf:'/skin/nekretnine/images/banner/2010/12/kupres.swf',width:320,height:170 });
//	$('#banner_4').flash({swf:'/skin/nekretnine/images/banner/2010/12/emmezeta.swf',width:320,height:170 });
	
});


tmpSlider				= function()
{
	$slider				= $('li#slider_cnt_tmp').get(0);
	if( typeof($slider) == "undefined" )
	{
		return;
	}
	$value				= parseInt( $slider.style.marginLeft )*(-1);
	if( isNaN($value) )
	{
		$value			= 0;
	}
	
	$rpt				= $value/150;
	
	if( $rpt >= 16 )
	{
		$rpt			= 0;
	}
	else
	{
		$rpt++;
	}
	
	$('li#slider_cnt_tmp').animate({'margin-left':'-'+($rpt*150)+'px'}, 1000, function(){
		$('li#slider_cnt_tmp').css({'margin-left':'-'+($rpt*150)+'px'})
		setTimeout( 'tmpSlider()', 1500 );
	});
}
