$(document).ready(function() {
	/* tell a friend submit */

    $('#tell-a-friend-submit').click(function() {       
         
        //Get the data from all the fields
        var name = $('#tell-a-friend-form input[name=name]');
        var email = $('#tell-a-friend-form input[name=email]');
        var friendEmail = $('#tell-a-friend-form input[name=friend-email]');
        var link = $('#tell-a-friend-form input[name=link]');
        var message = $('#tell-a-friend-form textarea[name=message]');
 		error = false;
        //Simple validation to make sure user entered something
        //If error found, add form-error class to the text field
        if (name.val()=='') {
            name.addClass('form-error');
            error = true;
        } else name.removeClass('form-error');
         
        if (email.val()=='') {
            email.addClass('form-error');
            error = true;
        } else email.removeClass('form-error');
        
        if (friendEmail.val()=='') {
            friendEmail.addClass('form-error');
            error = true;
        } else friendEmail.removeClass('form-error');
        
        if (message.val()=='') {
            message.addClass('form-error');
            error = true;
        } else message.removeClass('form-error');
         
         if (error){
         	return false;
         }
        //organize the data properly
        var data = 'name=' + name.val() + '&email=' + email.val() + '&friend-email=' + friendEmail.val() + '&link='
        + link.val() + '&message='  + encodeURIComponent(message.val());
       
        //disabled all the text fields
        $('.text').attr('disabled','true');
         
        //show the loading sign
        //$('.loading').show();
         
        //start the ajax
        $.ajax({
            //this is the php file that processes the data and send mail
            url: "http://images.humanspectra.com/tell_a_friend.php",
             
            //GET method is used
            type: "POST",
 
            //pass the data        
            data: data,    
             
            //Do not cache the page
            cache: false,
             
            //success
            success: function (html) {             
                //if process.php returned 1/true (send mail success)
                if (html==1) {                 
                    //hide the form
                   $('#tell-a-friend-form').slideUp('fast');                
                   $('#flash').html('<div class="flash flash_success">Your message has been sent successfully.</div>');
                //if process.php returned 0/false (send mail failed)
                } else alert(html);              
            }      
        });
         
        //cancel the submit button default behaviours
        return false;
    }); 	
	
	$("#tell-a-friend-toggle").toggle(function(e) {
		$("#tell-a-friend-form").slideDown('fast');
		e.preventDefault();
		return false;
		
	}, function(e) {
		$("#tell-a-friend-form").slideUp('fast');
		e.preventDefault();
		return false;
	});

	$('#search-form').bind('submit',function(e){
		/*if($('#search-string').attr('value')){
			searchstring = $('#search-string').attr('value');
			horizontal = $('#search-horizontal').attr('checked')? 1 : 0;
			vertical = $('#search-vertical').attr('checked')? 1 : 0;
			panorama = $('#search-panoramic').attr('checked')? 1 : 0;
			modelreleased = $('#search-modelreleased').attr('checked')? 1 : 0;
			url = "/search/" + searchstring + "/" + panorama + "/" + horizontal + "/" + vertical + "/" + modelreleased + "/" ;
			// send 'em off
			document.location = url;
		} else {
			alert('Search string can\'t be blank');
		}*/
		
		searchstring = $('#search-string').attr('value');
		horizontal = $('#search-horizontal').attr('checked')? 1 : 0;
		vertical = $('#search-vertical').attr('checked')? 1 : 0;
		panorama = $('#search-panoramic').attr('checked')? 1 : 0;
		modelreleased = $('#search-modelreleased').attr('checked')? 1 : 0;
		if($('#search-string').attr('value')){
			url = "/search/" + searchstring + "/" + panorama + "/" + horizontal + "/" + vertical + "/" + modelreleased + "/" ;
		} else {
			url = "/search/_/" + panorama + "/" + horizontal + "/" + vertical + "/" + modelreleased + "/" ;
			
		}
		// send 'em off
		document.location = url;
				
		return false;
	});
	// Make all buttons to themeable buttons
	$('#page-submit').button({
		icons: {
			primary: 'ui-icon-contact',
			secondary:'ui-icon-triangle-1-e'
		}
	});
	$('#page-cancel').button({
		icons: {
			primary: 'ui-icon-close'
		}
	});

	$("a").bind('click', function(e){
		e.blur;
	});
	
	$('a.to_lightbox').lightBox();

	$("a.sr_view_larger, a.sr_image_details,a.sr_download_preview, a.sr_add_to_project, a.sr_remove_from_project, a.sr_add_to_cart").tipsy({
		gravity: 'n'
	});
	
	$("img.sr_thumb").tipsy({
		/*grativy: $.fn.tipsy.autoNS,*/
		gravity: $.fn.tipsy.autoNS,
		html: true
	});

	$('a.preview_link').bind('click',function(event){
		event.preventDefault();
	    $.get(this.href,{},function(response){ 
	 	   $('#preview-pane').html(response)
	    })
	 });
});

function ajax_validate(e, controller, fieldName, fieldValue){
	controller = controller? controller : 'users';
	
	fieldName = fieldName? fieldName : $(e).attr('id');
	fieldValue = fieldValue? fieldValue : $(e).val();
	
	if(fieldValue.length > 0){	
		$.post('/'+controller+'/ajax_validate/', {
			field: fieldName,
			value: fieldValue
		},
		function(error){
			if(error.length != 0){
				$(e).removeClass('form-success');
				$(e).next('.error-message').remove();
				$(e).addClass('form-error');
				$(e).after('<div class="error-message" id="'+fieldName+'-invalid">' + error + '</div>');
			}else{
				$(e).removeClass('form-error');
				$(e).next('.error-message').remove();
				$(e).addClass('form-success');
			}
		});
	}
}



function submit_search(){
	var searchstring = encodeURI(document.getElementById('search-string').value);
	var horizontal = document.getElementById('search-horizontal').checked? 1 : 0;
	var vertical = document.getElementById('search-vertical').checked? 1 : 0;
	var panorama = document.getElementById('search-panoramic').checked? 1 : 0;

	url = "/search/" + searchstring + "/" + panorama + "/" + horizontal + "/" + vertical + "/" ;

	document.location = url;
}
