// creating tabs on main page


$(document).ready(function() {
	$('.txt').focus();

 var hash=location.hash;
 if (hash){
     $('#searchinput').val(unescape(hash.substr(1)));
 }
 $('#votebroken').click(function(){
var obj=$(this);
    $.post( '/in/file.php', {action:'votebroken', id: obj.attr('fileid')} ,
      function( data) {
          obj.parent().html(data);
      }
    );
}
);



    
});


function badImgReport(img){
    $.post( '/in/file.php', {action:'badimgreport', id: $(img).attr('imgid'), src: img.src} ,
      function( data) {
      }
    );


}


// 
var goodtogo=false;
var replyaction=false;
function myio(data){
			$('body').css('cursor','wait');
        $.getJSON('/in/file.php?'+data, {}, function(json){
        	$('body').css('cursor','default');
        	if (json.goodtogo) {
        		goodtogo=true;
        		$('#cbox').html('');
        		$('#cbox').hide();
        	}        	
        	if (json.codebox) {
        		$('#cbox').show();
        		$('#cbox').html(json.codebox);
        	}
        	if (json.votebox) {
        		$('#votebox').html(json.votebox);
        	}
        	if (json.commbox) {
        		$('#commbox').html(json.commbox);
        	}
        	if (json.click){
        		document.getElementById(json.click).click();
        	}
        	if (json.codeok){
        		if (replyaction){
        			action=replyaction;
        			replyaction=false;
					document.getElementById(action).click();
        		}
        		
   	    	}

        });
	
}

function golink(link){
if (!goodtogo){
	doaction('action=golink',link.id);return false;
}	
 else return true;
}


function doaction1(data,itemid){
	replyaction=itemid;
	myio(data);
}
function doaction(action,id){
    //console.log(this);

}
//if (window.location.href.indexOf("forcecode=true")<0)
//myio('');
//else myio('action=getcode');



function navigate(setname,setvalue,unsetname){
    var vars = [], value,key, hash;
    var i=window.location.href.indexOf('#');
    if (i>0) newurl=window.location.href.substr(0,i);
        else newurl=window.location.href;
    var i=newurl.indexOf('?');
   	var m=true;
    if (i>0){
    var hashes = newurl.slice(i + 1).split('&');
    	newurl=newurl.substr(0,i);
    
    
    for(i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        if (hash[0]==unsetname){
        	
        } else {
        	if (m) {newurl+='?';m=false;} else newurl+='&';
        	newurl+=hash[0]+'=';
       	   	if (hash[0]==setname)	{newurl+=setvalue;setname=false} else newurl+=hash[1];
        }
    }
    };// else newurl=window.location.href;
    if (setname) {
    	if (m) {newurl+='?';m=false;} else newurl+='&';
    	newurl+=setname+'='+setvalue;
    }
    
    window.location=newurl;
    	
}



//You need an anonymous function to wrap around your function to avoid conflict
(function($){

    //Attach this new method to jQuery
    $.fn.extend({

        //This is where you write your plugin's name
        mySubmit: function(success,error) {

            //Iterate over the current set of matched elements
            return this.each(function() {



//            window.resubmitf=function (){
//                event.data.submit(event);
//            };
//            window.setError=function(str){
//                event.data.onerror(event.target,str);
//            }
//            event.preventDefault();
            var post={};
            var value='';
            var $this=$(this);
            $(this).children().each(function(){
                var name=$(this).attr('name');
                if (name){
                        value = $(this).val();
                    post[name]=value;
                }
            })
            url=$this.attr( 'action' );
            post.doclocation = escape(document.location);
            // Send the data using post and put the results in a div
            $.post( url, post ,
                function( data) {
                    var obj=$.parseJSON(data);
                    if (obj.jsredirect)
                        window.location=obj.jsredirect;
                    if (obj.script)
                        $.getScript(obj.script);
                    if (obj.error){
                        error($this,obj.error);
                    }
                    if (obj.html) {
                        success($this,obj.html);
                    }
                }
                );
            return false;










                //code to be inserted here

            });
        }
    });

//pass jQuery to the function,
//So that we will able to use any valid Javascript variable name
//to replace "$" SIGN. But, we'll stick to $ (I like dollar sign: ) )
})(jQuery);

$('.bs_action').live('click',function(event){

var id=$(event.currentTarget).attr('id');
switch (id){
    case 'bs_addalert':
        var show=function(){
        $.get('/bapi.php?action=getalertform&doclocation='+escape(document.location), function(data){
            $(event.currentTarget).append('<div id="email" title="Create an email alert">'+data+'</div>');
            $('#email').dialog({
                modal: true,
                close: function(){
                    $('#email').remove();
                }
            });
        $('#email input[name$="email"]').focus();
        });

        };
        if ('undefined' == typeof($().dialog)) $.getScript('/static/js/jquery-ui-1.8.13.custom.min.js',function(){
            show();
        })
        else show();

        break;
    case 'sayyes':
    case 'sayno':
    case 'saypartly':
        var obj=$(event.currentTarget).parent();
        var fileid=obj.attr('fileid');
        var qid=obj.attr('qid');
        var data = {action: 'survey', fileid: fileid, qid: qid, vote: id};
        $('#questionbox').remove();
        $.post('/in/file.php', data);
        
        break;
    case 'focuscomment':
        $('.lt-new-comment textarea').focus();
        $('.lt-new-comment textarea').fadeOut();
        $('.lt-new-comment textarea').fadeIn();
        $('#questionbox').remove();
        break;
    case 'mediavideo':
    case 'mediaaudio':
    case 'mediaimage':
    case 'mediadoc':
    case 'mediaall':
    case 'mediafilter':
        event.preventDefault();
        var path=$(event.currentTarget).attr('bs_path');
        var i=window.location.href.indexOf('?');
        if (i>0) path=path+window.location.href.substr(i);
        window.location=path;
    break;
}

});



function formhandler(form){
    var show =function(){
        var id=$(form).attr('id');
        switch (id){
            case 'bs_alertform':
            $(form).mySubmit(
                function(obj,data){
                    var dlg=$('#email');
                    dlg.html(data);
                    dlg.dialog({buttons: {"Ok": function() {dlg.dialog("close");}}});
                },
                function (obj,data){
                    $(obj.children('div:last')[0]).html('<div  class="ui-state-error ui-corner-all">'+data+'</div>');
                }
            );
                break;
        }
        
    };
        if ('undefined' == typeof(formworkspace)) $.getScript('http://lesstalks.com/static/js/formwork.js',function(){
            show();
        })
        else show();
}




