$(document).ready(function() {



    $('#Search').click(function() {
      $(this).val('');
    });

    $(".close").live('click',function(){
        $('#loginForm').dialog('close');
        return false;
    });
    $("#close3").live('click',function(){
        $('#newUserForm').dialog('close');
        return false;
    });

    $('#loginForm').dialog({ width: 500 , zIndex: 10, title: 'Logi sisse', autoOpen: false, modal: true});
    $('#newUserForm').dialog({ width: 500 , zIndex: 11, title: 'Logi sisse', autoOpen: false, modal: true});

    $("#clickLogin").live('click',function(){
        $('#loginForm').dialog('open');
        $('.ui-dialog-titlebar').hide();
        return false;
    });

    $("#newUser").live('click',function(){
        $('#newUserForm').dialog('open');
        $('.ui-dialog-titlebar').hide();
        return false;
    });



    $('#submitLogin').click(function() {
        mySubmit('#formLogin', base_url + "user/Login/");
        if ( $('#result').val() == 1 ){
            $('#loginForm').dialog('close');

            if ( $("#give").parents(".ui-dialog").is(":visible") == false){
                //window.location = base_url;
                location.reload();
            }else{
                location.reload(true);
                $('#give').dialog('open');

//                $('#result').load(window.location.href, function() {
//                    $('#give').dialog('open');
//                });
                
                
            }
        }

    return false;
    });

    $('#formNewUser').submit(function() {
        mySubmit('#formNewUser', base_url +"user/Reg/");
        if ( $('#result').val() == 1 ){
            $('#newUserForm').dialog('close');
            window.location = base_url + 'user/success';
        }

    return false;
    });
});



function clearForm(form) {
  // iterate over all of the inputs for the form
  // element that was passed in
  $(':input', form).each(function() {
 var type = this.type;
 var tag = this.tagName.toLowerCase(); // normalize case
 // it's ok to reset the value attr of text inputs,
 // password inputs, and textareas
 if (type == 'text' || type == 'password' || tag == 'textarea')
   this.value = "";
 // checkboxes and radios need to have their checked state cleared
 // but should *not* have their 'value' changed
 else if (type == 'checkbox' || type == 'radio')
   this.checked = false;
 // select elements need to have their 'selectedIndex' property set to -1
 // (this works for both single and multiple select elements)
 else if (tag == 'select')
   this.selectedIndex = -1;
  });
};

function mySubmit(formName, Url){
        $.ajax({
           type: "POST",
           url: Url,
           data: $(formName).serialize() ,
           async: false,
           success: function(msg){
               msg =jQuery.trim(msg);
             if (msg  == ''){
                $('#info').html('<span>Andmed salvestatud</span>');
                $("#info span").fadeOut(2000);
                clearForm(formName);
                $(formName).append('<input id="result" type="hidden" value="1" />');
                return true;
             }else{
                 alert ( msg );
             }
           },
           error: function (data){
                jAlert('<span>' + data.responseText + '</span>');

           }
         });
return false;
}

function myPost(Url){
    var msg = '';
        $.ajax({
           type: "POST",
           url: Url,
           success: function(msg){

           }
         });
return msg;
}


function dump(obj) {
    var out = '';
    for (var i in obj) {
        out += i + ": " + obj[i] + "\n";
    }
    alert(out);

}



function FlashInstalled(){
	result = false;
	if (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"]){
		result = navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin;
	}
	else if (document.all && (navigator.appVersion.indexOf("Mac")==-1)){
		// IE Windows only -- check for ActiveX control, have to hide code in eval from Netscape (doesn't like try)
		eval ('try {var xObj = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");if (xObj)	result = true; xObj = null;	} catch (e)	{}');
	}
	return result;
}

function FlashWriteAlvar(url,clicktag,width,height){
	document.write('<OBJECT classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" WIDTH=' + width + ' HEIGHT=' + height + '>');
	document.write('<PARAM NAME=movie VALUE="' + url + '?clickTAG=' + clicktag + '"> <PARAM NAME=menu VALUE=false> ');
	document.write('<PARAM NAME=quality VALUE=high> ');
	document.write('<PARAM NAME=wmode VALUE=transparent> ');
	document.write('<EMBED src="' + url + '?clickTAG='+ clicktag + '" menu=false wmode=transparent quality=high WIDTH=' + width + ' HEIGHT=' + height + ' TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></EMBED>');
	document.write('</OBJECT>');
	document.write('&nbsp;&nbsp;&nbsp;');
}
