
$(document).ready(function() {

	
		//$.localScroll({});

		//add animation to bootwstrap dropdown
		$('.dropdown').on('show.bs.dropdown', function(e){
			$(this).find('.dropdown-menu').first().stop(true, true).slideDown();
		});

		// ADD SLIDEUP ANIMATION TO DROPDOWN //
		$('.dropdown').on('hide.bs.dropdown', function(e){
			$(this).find('.dropdown-menu').first().stop(true, true).slideUp();
		});



		//cleanup modals after close
		$('body').on('hidden.bs.modal', '.modal', function () {
		  $(this).removeData('bs.modal');
		});

		//hide all widget's title when its empty
		$(".widget h3").each(function(e){
			if ($(this).html() == ""){
				$(this).css("display" , "none");
			}
		});


		//capcha
		$(".capcha-img").click(function(e){
			$(this).find("img").attr("src" , $(this).attr("href") + "?" + $.now() );		
			return false;
		});

		

		
		//enable uniform for checkboxes, radio and range elements
		if(jQuery().uniform){
			$('input[type=checkbox],input[type=radio],input[type=range]').uniform();
		}

		//enable chosen for all selects except the ones with .ignore css class
		if(jQuery().chosen) {
			$('select:not(.ignore)').chosen();
		}

		//enable mCustomScrollbar
		if(jQuery().mCustomScrollbar) {
			$(".nicescroll").mCustomScrollbar({
				axis:"y" // vertical and horizontal scrollbar
			});			
			//$('.nicescroll').niceScroll();
		}

		//enable prettyphoto gallery
		if(jQuery().prettyPhoto){
			$("a[rel^='prettyPhoto']").prettyPhoto({social_tools:false});

		}

		//add current panelt to bootstrap acordeons for easier control.
		$('.panel-collapse').on('show.bs.collapse',function(){
			//$(this).prev('.panel-heading').addClass("active-panel");			
			$(".panel.current-panel").removeClass("current-panel");
			$(this).parent().addClass("current-panel");
		});

		$('.panel-collapse').on('hide.bs.collapse',function(){
			//$(this).prev('.panel-heading').removeClass("active-panel");
			$(this).parent().removeClass("current-panel");
		});

		
		//all forms with class ajax will return the response in responsebox element
		var ajaxForm = false;
		$(document).on(
			"submit" , 
			"form.ajax",
			
			function(e){
				e.preventDefault();
	
				var form = $(this);

				if(ajaxForm!= false)
				  ajaxForm.abort();


				ajaxForm = $.ajax({

					url:form.attr("action"),
					type:form.attr("method"),

					data:form.serialize(),
				}).done(function(msg){
					
					ajaxObject = false;

					//redirect message
					if (msg.indexOf("redirect:") != -1){
						window.location.href = msg.replace("redirect:" , "" );
					} else {

						//capcha reload
						if (msg.indexOf("captcha:") != -1){
							form.find(".capcha-img").trigger("click");

							form.find(".responsebox").html(msg.replace("captcha:" , ""));
						} else {

							//execute function
							if (msg.indexOf("execute:") != -1 ) {
								eval( msg.replace("redirect:" , "" ) );
							} else {
								//full message
								form.find(".responsebox").html(msg);
							}
						}

					}

				});				
			}
		);

});
