var $j = jQuery.noConflict();

function InitJs()
{
	 $j("[title]").each(
		   		function(i, el){									
					$j(el).simpletooltip();
				}
		   );
			
			$j(".treeview").each(function(i, el){
				$j(el).treeview({
					persist: "location",
					collapsed: true,
					unique: true
				});
			});
		   
		   $j(".temporary").each(
		   		function(i,el){
					$j(el).oneTime(10000, function() {					
					    $j(this).hide();
					  });
				}
		   );
		   
		   $j(".message").each(
		   		function(i,el){
		  			$j(el).dialog({width:900, height: 200, title:$j(el).attr('window'), modal: true, draggable:true,   resizable:true });
					$j(el).dialog( 'open' );
				}
		   );
		   
		    $j(".accordion").each(
		   		function(i,el){
		  			$j(el).accordion({autoHeight: false, active:false, event:'mouseover'});
				}
		   );
		   
		   $j(".progressbar").each(
		   		function(i,el){
		  			$j(el).progressbar({ value: $j(el).attr("value")?$j(el).attr("value"):0 });
				}
		   );
		   
		   $j(".slider").each(
		   		function(i,el){
		  			$j(el).slider({
						value: $j(el).attr("value") ? $j(el).attr("value") : 0,
						
						 slide: function(event, ui) { $j("#"+el.id+"_field").val(ui.value);}

					});
				}
		   );
		   
		   $j("*.popup").each(function(i,el){
		   		$j(el).click(
					function()
					{
						p_id="#"+$j(this).attr("window");					
						if($j(p_id).length<=0)
							$j(this).after("<div id='"+$j(this).attr("window")+"' style='display:none'></div>");															
						p_width=parseInt(($j(this).attr("window_width")?$j(this).attr("window_width"):100));
						p_height=parseInt($j(this).attr("window_height")?$j(this).attr("window_height"):100);
						p_resizeable=($j(this).attr("window_resizeable")?$j(this).attr("window_resizeable")=="true":true);
						p_draggable=($j(this).attr("window_draggable")?$j(this).attr("window_draggable")=="true":true);
						p_modal=($j(this).attr("window_modal")?$j(this).attr("window_modal")=="true":true);
						p_title=($j(this).attr("window_title")?$j(this).attr("window_title"):"");
						p_link=($j(this).attr("window_link")?$j(this).attr("window_link"):'');
						 
						this.dialog=$j(p_id).dialog({
							width:p_width,
							open: function(event, ui) {
								if(p_link){
									$j(p_id).load(p_link)
								} 
							}, 
							close:function(event,ui){
								$j(p_id).dialog('destroy');
							},
							height: p_height, 
							title:p_title, 
							modal: p_modal, 
							draggable:p_draggable,  
							resizable:p_resizeable });
						this.dialog.dialog( 'open' );
					}
				);				   		
		   });
		   
		   $j("input.calendar").each(function(i,el){
		   		$j(el).datepick({ dateFormat: 'yy-mm-dd', yearRange:"-90:+10" });
		   });
		   
		   $j("input.spinner").each(function(i,el){
		   		$j(el).spinner();
		   });
		   $j("div.ddsmoothmenu").each(function(i,el){
		   		ddsmoothmenu.init({
					mainmenuid: el.id,				
					contentsource: "markup" 
				})
		   });
		   
		   $j("*.tabs_view").each(function(i,el){
		   		
			   	options={		   			
			   			select:function(event, ui){
			   				jQuery(ui.tab).addClass('tabs_loader');			   				
			   			},		   			
			   			show:function(event, ui){
			   				jQuery(ui.tab).removeClass('tabs_loader');
			   				}
			   	};
				if($j(el).attr("select"))
				{		
					options.selected=$j(el).attr("select");
				}
				$j(el).tabs(options);
				$j(el).show();
		   });
		   
		   $j("*.slideshow").each(function(i,el){
		   		$j(el).carousel({
			      hide: 'fadeOut', // jquery show / hide effect method name
			      show: 'fadeIn', // jquery show / hide effect method name
			      duration: 500, // duration of wait in milliseconds
			      speed: 2000, // speed in milliseconds, 'slow' or 'fast',
			      seed: 5 // length of random classname applied to item to keep things all nice and seperate
			              // warning: setting this to something ridiculous will make it take ages to load.
			    });
				$j(el).show();
			});
		   
		   $j("*.expander").each(function(i,el){
		   		$j(el).click(
					function(){
						$j(this).toggleClass('minimized');$j(this).toggleClass('maximized');
						if ( $j(this).attr("expand") && $j("#"+$j(this).attr("expand")).get()!="") {
							if ($j("#"+$j(this).attr("expand")).is(':visible'))
								$j("#"+$j(this).attr("expand")).hide();
							else 
								$j("#"+$j(this).attr("expand")).show();
						}
						else {
							if ($j(this).next().is(':visible')) 
								$j($j(this)).next().hide();
							else 
								$j($j(this)).next().show();
						}
					}
				);	   		
		   });
		   
		   
		   $j("[hint]").each(function(i,el){
		   		$j(el).inputHintBox({attachTo:$j(el).parent(),className:'hint_box',source:'attr',attr:'hint',incrementLeft:0,incrementTop:0});			
				
		   });
}

// tooltip init
$j(document).ready(function() {
		
       InitJs();
});

function Validation(id,options)
{
	this.id=id;
	this.options=options;
	
	$j("#"+id).validate();
}

function show_loader(where)
{
	if(where)
	{
		w=jQuery(where).width();
		h=jQuery(where).height();		
		jQuery(where).prepend('<div class="div-ajax-loader"></div>')
		jQuery(where+' .div-ajax-loader').css({			
			position:'absolute',
			'z-index':'20000000',
			'text-align':'center',
			opacity:0.8
		}).width(w).height(h);
	}
}

function ajax_load(url, pars, where, evalonsuccess) 
{
	show_loader(where);
	jQuery.ajax({
			url: url,
			data: pars,
			cache: true,
			type:'POST',
			success: function(response){
				if(where!="")
				{
					jQuery(where).html(response);					
				}
				eval(evalonsuccess);
			}
		});
}


function go_to(url)
{
	location.href=url;
	return false;
}

function edit(url,id)
{
	go_to(url+"?a=edit:"+id);
}

function del(url)
{
	if(confirm("Sunteti sigur ca doriti sa stergeti?"))
	{
		go_to(url);
	}
}

function cnf(message)
{
	return confirm(message);
}

function cart_add(product_id,quantity)
{
	if(product_id && quantity)
		go_to(root+"companii/cart?a=add&product_id="+product_id+"&quantity="+quantity);
}

function cart_save(product_id,quantity)
{
	if(product_id && quantity)
		go_to(root+"companii/cart?a=save&product_id="+product_id+"&quantity="+quantity);
}

function cart_delete(product_id,quantity)
{
	if(product_id)
		go_to(root+"companii/cart?a=delete&product_id="+product_id+"&quantity="+quantity);
}

function cart_empty()
{
	go_to(root+"companii/cart?a=empty");
}

function form_check_all(id, elem){
	$j("*:checkbox").each(
		
		function(i,el){
			el.checked=elem.checked;
		}
	);	
}

function check_all(form_id)
{
	$j("#"+form_id+" input:checkbox").each(			
			function(i,el){
				el.checked=true;
			}
		);	
}

function uncheck_all(form_id)
{
	$j("#"+form_id+" input:checkbox").each(			
			function(i,el){
				el.checked=false;
			}
		);	
}

function getProgress(id){
	jQuery.getJSON(root+"?progress_key=" + jQuery("#" + id).attr('key'), "", function(data){
		procent = parseInt((data.current * 100 / data.total));
		jQuery("#" + id + "> .bar").width(procent + "%");
		jQuery("#" + id + "> .bar").html(procent + "%");
		jQuery("#" + id + "> .downbar> .speed").html("Av. Speed: " + parseInt(data.speed / 1024) + "Kb/s");
		jQuery("#" + id + "> .downbar> .status").html("Uploaded/Total:"+parseInt(data.current / 1024) + "Kb/" + parseInt(data.total / 1024) + "Kb");
		if (procent < 100) 
			setTimeout("getProgress('" + id + "')", 1000);
	});
}

function startProgress(id){
    jQuery("#"+id).addClass('up_progress')
		.show();	
	jQuery("#"+id).append("<div class='bar'></div>");
	jQuery("#"+id).append("<div class='downbar'><div class='speed'></div><div class='status'></div></div>");
	jQuery("#"+id).append("<div class='clear'></div>");
	setTimeout("getProgress('"+id+"')", 1000);
}
function clickclear(thisfield, defaulttext) {
	if (thisfield.value == defaulttext) {
	thisfield.value = "";
	}
}

function clickrecall(thisfield, defaulttext) {
	if (thisfield.value == "") {
	thisfield.value = defaulttext;
	}
}
