
jQuery(document).ready(function(){
	
	$( "#btn_se" ).button();
	
	//alert ('document is ready');
	$( ".jq_button" ).button();
	$( ".jq_btn" ).button();
	
	
	$( "#dialog" ).dialog();
	$( ".demo button:first" ).button({
            icons: {
                primary: "ui-icon-locked"
            },
            text: false
        }).next().button({
            icons: {
                primary: "ui-icon-locked"
            }
        }).next().button({
            icons: {
                primary: "ui-icon-gear",
                secondary: "ui-icon-triangle-1-s"
            }
        }).next().button({
            icons: {
                primary: "ui-icon-gear",
                secondary: "ui-icon-triangle-1-s"
            },
            text: false
        });
	
	$( "#tabs" ).tabs({
			ajaxOptions: {
				error: function( xhr, status, index, anchor ) {
					$( anchor.hash ).html(
						"Couldn't load this tab. We'll try to fix this as soon as possible. " +
						"If this wouldn't be a demo." );
				}
			}
		});
	
	$( ".sumbit_button" ).button();
	// Footer
	$("#footer_acc").accordion({ 
		header: "h5",
		autoHeight: false,
		collapsible: true
	 });
	$("#footer_acc").accordion('activate', 0);
	$("#latest_news_acc").accordion({ 
		header: "h5",
		autoHeight: false,
		collapsible: true
	 });
	$("#latest_news_acc").accordion('activate', 0);
	$("#navigation_acc").accordion({ 
		header: "h3",
		autoHeight: false,
		collapsible: true
	 });
	$("#navigation_acc").accordion('activate', 0);
	$("#all_keywords").accordion({ 
		header: "h5",
		autoHeight: false,
		collapsible: true
	 });
	//$("#all_keywords").accordion('activate', 0);
	$("#navigation_acc_expanded").accordion({ 
		header: "h3",
		autoHeight: false,
		collapsible: true
	 });
	$( "#keywords_acc" ).accordion({
			collapsible: true,
			autoHeight: false,
			create: function(event, ui) { 
				//alert("created");
				$("#accordion").css('visibility', 'visible');
				$("#filter").css('visibility', 'visible');
			}
		});
	$("#keywords_acc").accordion('activate', 0);
	$("#keywords_acc_wrapper").accordion({ 
		header: "h2",
		autoHeight: false,
		collapsible: true
	 });
	 
	 $("#all_page").css('height', 'auto');
	 $("#all_page").css('visibility', 'visible');
	 
	 $("#footer_box").css('height', 'auto');
	 $("#page_body").css('height', 'auto');
	 $("#page_right").css('height', 'auto');
	 
	 
	 //alert ('show page');
	
	function log( message ) {
			$( "<div/>" ).text( message ).prependTo( "#log" );
			$( "#log" ).attr( "scrollTop", 0 );
		}

		$( "#keyword_ac" ).autocomplete({
			source: "search.php",
			minLength: 2,
			select: function( event, ui ) {
				log( ui.item ?
					"Selected: " + ui.item.value + " aka " + ui.item.id :
					"Nothing selected, input was " + this.value );
					
					// simulates similar behavior as an HTTP redirect
					var str = '/online_marketing/spoonfeed/?keyword='+ui.item.value;
					document.location.href = str;
					
					//$("#show_keyword").load("keyword_details.php", {keyword: ui.item.value}, function(){
						//alert("The last 25 entries in the feed have been loaded");
					//});
			}
		});
		$( "#keyword_ac" ).focus();
		
		//$( "#show_keyword" ).hide();
	
	//alert("contact form ready");
	$(".oops#validation").hide();
	$(".oops#sending").hide();
	$(".oops#success").hide();
	$(".oops#failure").hide();
	$(".post-error").hide();
	
	var projects = [
			{
				value: "chamonix",
				label: "Chamonix",
				desc: "All the links from chamonix",
				icon: "images/logos/bing-logo.jpg"
			},
			{
				value: "jquery-ui",
				label: "jQuery UI",
				desc: "the official user interface library for jQuery",
				icon: "jqueryui_32x32.png"
			},
			{
				value: "sizzlejs",
				label: "Sizzle JS",
				desc: "a pure-JavaScript CSS selector engine",
				icon: "sizzlejs_32x32.png"
			}
		];

		$( "#project" ).autocomplete({
			minLength: 0,
			source: projects,
			focus: function( event, ui ) {
				$( "#project" ).val( ui.item.label );
				return false;
			},
			select: function( event, ui ) {
				$( "#project" ).val( ui.item.label );
				$( "#project-id" ).val( ui.item.value );
				$( "#project-description" ).html( ui.item.desc );
				$( "#project-icon" ).attr( "src", "images/" + ui.item.icon );

				return false;
			}
		})
		.data( "autocomplete" )._renderItem = function( ul, item ) {
			return $( "<li></li>" )
				.data( "item.autocomplete", item )
				.append( "<a>" + item.label + "<br>" + item.desc + "</a>" )
				.appendTo( ul );
		};

	$(".button-create").click(function() {  
		$(".oops").hide();
		$(".post-error").hide();
		
		var enquiry = $(".comment-textarea#contact_enquiry").val();  
		if (enquiry == "") {
			$(".post-error#contact_enquiry_error").show();  
			$(".comment-textarea#contact_enquiry").focus();  
			var isError = true; 
		}
		
		
		
		var email = $(".input-post#contact_email").val();  
		if (email == "") {
			$(".post-error#contact_email_error").show();  
			$(".input-post#contact_email").focus();  
			var isError = true;
		}else{
			var str=email;
			var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
			if (!filter.test(str)){
				$(".post-error#contact_email_error").show();  
				$(".input-post#contact_email").focus();  
				var isError = true;
			}	
		}
		
		var name = $(".input-post#contact_name").val();  
		if (name == "") {
			$(".post-error#contact_name_error").show();  
			$(".input-post#contact_name").focus();  
			var isError = true;
		}
		
		if (isError){
			$(".oops#validation").show();
		}else{
			$(".oops#sending").show();
			$(".post-input").hide();  
			$(".post-label").hide();  
			$(".button-create").hide();  
  			var phone = $(".input-post#contact_phone").val();
			$.ajax({
			type : 'POST',
			url : '/php/send_contact.php',
			dataType : 'json',
			data: {
				name : name,
				email : email,
				phone : phone,
				enquiry : enquiry
			},
			success : function(data){
				//alert (data.msg);
				$(".oops#sending").hide();
				$(".oops#success").show();
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				//alert ('boo, error');
				$(".oops#sending").hide();
				$(".oops#failure").show();
				$(".post-input").show();  
				$(".button-create").show();  
				$(".post-label").show(); 
			}
		});

		}
		
		// only send with Ajax
		return false;
	  
  	}); 
	
});
function on_page_all_done(){
		alert ('page ready now');
};
//on_page_all_done();
