/**
 * @author al
 */

jQuery(document).ready(function($){
	//$("#top #nav li a").hover(function(){
	//	$(this).parent("li").css({'background': 'transparent url(../images/menu-sprite.png) no-repeat scroll center bottom'});
		//$(this).parent("li").css({'border-top': '1px solid lime'});
	//},
	//function(){
	//	$(this).parent("li").css({'background': 'transparent url(../images/menu-sprite.png) no-repeat scroll center top'});
	//	$(this).parent("li").css({'border-top': '1px solid red'});
	//});
	$('.jCarouselLite li').each(function(i, o){
		$(o).css('backgroundImage', 'url(/images/thumbs/' + (i+1) + '.jpg)');
		$(o).find('img').wrap('<a href="/images/large/'  + (i+1) + '.jpg" />');
	});
	

	$("#footer .links a[href]").click(function(){
		var dHTML = '<div id="dialog" title="Login to Admin?"><img class="logofloatleft" src="/images/easyeditorlogo.png" alt="Easy Editor" /><p>Enter your username/password:</p>';
		dHTML +='<form method="POST" action="#"><fieldset><label for="username">Username</label><input type="text" name="username" id="username" class="text ui-widget-content ui-corner-all" />';
		dHTML +='<label for="password">Password</label><input type="password" name="password" id="password" value="" class="text ui-widget-content ui-corner-all" /></fieldset><input type="hidden" name="actions" value="login"></form></div>';
		$('body').append(dHTML);
		$("#dialog").dialog({
			bgiframe: true,
			resizable: false,
			height:400,
			width:400,
			modal: true,
			overlay: {
				backgroundColor: '#000',
				opacity: 0.5
			},
			buttons: {
				Cancel: function() {$(this).dialog('close')},
				Submit: function() {
					var form = $("#dialog form");
					
					form.attr("action", window.location.href);
				
					form.submit();				
				}
			},
			close: function(event, ui) {$(this).remove();}
		});
		return false;
	});	
	
	$("#search1").ajaxForm({
		beforeSubmit: function(data, form, options){
			var area = form.find('select[name="area"]').val();
			var str = form.serialize();
			window.location.href= "/search/" + area + "/?"+ str;
			return false;
		}
	});
	$("#search1 a.button_link").click(function(){
		$("#search1").submit();
	});
	
	$(".jCarouselLite").jCarouselLite({
   		btnNext: ".next",
    	btnPrev: ".prev"
	});
	$('.jCarouselLite li a').fancybox();
	var ajaxFormOptions = { 
        target:        '#form-response',   // target element(s) to be updated with server response 
        beforeSubmit:  showRequest,  // pre-submit callback 
        success:       showResponse,  // post-submit callback 
		url:"/lib/lasso/contact.lasso"      // override for form's 'action' attribute 
        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
       // dataType: 'json'      // 'xml', 'script', or 'json' (expected server response type) 
        //clearForm: true        // clear all form fields after successful submit 
        //resetForm: true        // reset the form after successful submit 
 
        // $.ajax options can be used here too, for example: 
        //timeout:   3000 
    }; 
	var message ="enter phone no...";
	function showRequest(formData, jqForm, options){
		if(!formData[0].value || formData[0].value=="" ||formData[0].value==message ){
			$('#form-response').append('<br /><span class="red">Please enter your phone number</span>');
			return false;
		}
	}
	function showResponse(responseText, statusText){
		$("#call-back").hide()
	}
	
	$('#call-back').ajaxForm(ajaxFormOptions);
	
	$('#call-back input[name="phone_no"]').keypress(function(e){
		 if( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)){
     		return false;
  			}
	})
	$('#call-back input[name="phone_no"]').focus(function () {
		$(this).val("");
		$(this).css('color', 'black');
	});
	$('#call-back input[name="phone_no"]').blur(function () {
		if($(this).val()==""){
			$(this).val(message);
			$(this).css('color', '#ABACAB');
		}
	});
	//////////////////////// Scripts routine ////////////////////////
	/*loads scripts of type text/plain and evaluates them and, once loaded, 
	 * it evaluates the contents. This means <script> tages can have 
	 * both a src and contents
	 */
	var scripts = $('script[type="text/plain"]'); ///scripts must be text/plain or jQuery/Jaxer will run them on the serverside
	jQuery.each(scripts, function(i, val){
		var script=this;
		 //if theres actual script rather than just a 
		var scriptText = script.text;
		if($(this).attr('src') && $(this).attr('src')!=''){
			var parent = $(script).parent()
			jQuery.getScript($(this).attr('src'), function(data, i){
				//console.dir(script.parent());
				
				eval(scriptText);
			});
		}else{
			eval(scriptText);
		}
	});
	/////////////////////////////////////////////////////////////////
	
});
