/**
 * @author Sergey Galitskiy 
 */

 
function focused(obj,defaultValue){
	if (obj.attr('value') == defaultValue) {
		obj.attr('value','');
	}
}
function blured(obj,defaultValue){
	if (obj.attr('value').replace(/ /g,'')=='') {
		obj.attr('value',defaultValue);
	}
}

function ajaxFileUpload(start_url){
		$(".overflow")
            .ajaxStart(function(){
                $(this).height($('.inLeft').height()).show();
            })
            .ajaxComplete(function(){
                $(this).hide();
        });

		$.ajaxFileUpload({
				url:'/en/ajax/',
				secureuri:false,
				fileElementId:'file',
				dataType: 'json',
				data:{description: $('#description').val(), upload: $('#upload').val()},
				success: function (data, status){
					if(typeof(data.error) != 'undefined'){
						if(data.error != ''){
							$('.errorMessage').text(data.error).show();
						} else {
                            // DO ACTION ON UPPLOAD SUCCESSFULLY
                            location.href = start_url;
                            //alert(data.msg);
						}
					}
				},
				error: function (data, status, e) {
					$('.errorMessage').text(e).show();
				}
			})
		return false;
	}

$(document).ready(function(){
	try{
        $('.hsubmit').hover(
          function(){
              var imgAttr = $(this).attr('src');
              imgAttr = imgAttr.substr(0, imgAttr.length-4);
                $(this).attr('src', imgAttr+'_hover.gif');
          },
          function(){
               var imgAttr = $(this).attr('src');
              imgAttr = imgAttr.substr(0, imgAttr.length-10);
                $(this).attr('src', imgAttr+'.gif');
          }
        )

        $('input#file').hover(
            function(){
                var img = $(this).next('img')
                var imgAttr = img.attr('src');
                imgAttr = imgAttr.substr(0, imgAttr.length-4);
                img.attr('src', imgAttr+'_hover.gif');
            },
            function(){
                var img = $(this).next('img');
                var imgAttr = img.attr('src');
                imgAttr = imgAttr.substr(0, imgAttr.length-10);
                img.attr('src', imgAttr+'.gif');
            }
        )

	jQuery.validator.addMethod(
	  "selectNone", function(value, element){ 
	    if (element.value == "none"){ 
			return false;
	    } else 
		    return true; 
	}, 
	  "Please select an option." 
	); 


     $('#forgot').validate({
        onsubmit:true,
		onfocusout: false,
		onkeyup:false,
		focusInvalid:false,
		errorElement:'div',
        rules : {
          'email':{
             selectNone:true,
              email:true
          }
        }
      })

   $('#contactForm').validate({
        onsubmit:true,
		onfocusout: false,
		onkeyup:false,
		focusInvalid:false,
		errorElement:'div',
        rules : {
          'fname':{
             selectNone:true
          },
          'telephone':{
             selectNone:true
          },
          'msg':{
             selectNone:true
          }
        },
       invalidHandler: function(form, validator) {
			var destination=0;
			if (validator.numberOfInvalids()>0){
				for (var key in validator.errorList) {
                    destination = (destination==0 ? $(validator.errorList[key].element).offset().top-30 : destination);
				}
              if($.browser.safari){
                $('body').animate( { scrollTop: destination }, 1100 );
              }else{
                $('html').animate( { scrollTop: destination }, 1100 );
              }
           }
       }
   })
	} catch (e){
		alert(e)
	}
})

