
//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;
var messageStatus = 0;

//loading popup with jQuery magic!
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupContact").fadeIn("slow");
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#popupContact").fadeOut("slow");
		popupStatus = 0;
	}
}

//centering popup
function centerPopupEx(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupContact").height();
	var popupWidth = $("#popupContact").width();
	//centering
	$("#popupContact").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});
	
}

function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupContact").height();
	var popupWidth = $("#popupContact").width();
	//centering
	$("#popupContact").css({
		"position": "absolute",
		"top": windowHeight/4-popupHeight/2,
		"left": windowWidth/3-popupWidth/2
	});
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});
	
}

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   //alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   //alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    //alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    //alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    //alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    //alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    //alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
	}


function registerForm() {
  alert('understood!');
  return false;
}

function showCustomMsgBox() {
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupContact").height();
	var popupWidth = $("#popupContact").width();
	//centering
	$("#object").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
    
	//loads message only if it is disabled
	if(messageStatus==0){
		$("#object").css({
			"opacity": "0.9"
		});
		/*$("#object").fadeIn("slow");
		$("#close_message").fadeIn("slow");*/

		//first slide down and blink the alert box
		$("#object").animate({ 
        top: "100px"
      }, 2000 ).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100);
		messageStatus = 1;
	}
    
   
    //close the message box when the cross image is clicked 
    $("#close_message").click(function()
	{
	   $("#object").fadeOut("slow");
	   messageStatus = 0;
	});
}

function rateBlog(bid, rate) {
	//alert('sending');
	var myurl = 'http://clement.bubuface.com/blog/rate/'+bid+'/'+rate;
	$.ajax({
    url: myurl,
    type: 'POST',
    dataType: 'xml',
    timeout: 1000,
    error: function(){
        //alert('Error loading XML document');
		//alert('thx for rating!');
		showCustomMsgBox();
    },
    success: function(xml){
        // do something with xml
		//alert('cool with loading XML document');
		//alert('thx for rating!');
		showCustomMsgBox();
    }
    });

	/*$.post(myurl, {
    blogid: bid,
    brate: rate
}, function() {
    alert('thx for rating!');
});*/

	return false;
}

function processShareForm(form) {
	var eml = $("#blogShareEmail").val();
	if(eml == null || eml == "" || echeck(eml)==false) {
	     alert('email required');
	     return false;
	} 
    //later we will use jquery ajax support
	return true;
}

function showCoolPopup(blogId) {
	//set blog id
	$("#blogShareId").attr('value', blogId);
	//centering with css
	centerPopup();
	//load popup
	loadPopup();
}

function strContains(str, toFind) {
	var pos = str.indexOf(toFind);
	if(pos >=0)
		return true;
	else 
		return false;
}

$(document).ready(function()
{
    /*{ 
        $("#myTable").tablesorter(); 
    } */
	$("#tcalendar").datepicker({ 
                 onSelect: function(date) { 
        } 
    }); 

	$("#commentForm").validate();
    $("#blogShareForm").validate();
    
    //LOADING POPUP
	//Click the button event!
	/*$("#button").click(function(){
		//centering with css
		centerPopup();
		//load popup
		loadPopup();
	});*/
				
	//CLOSING POPUP
	//Click the x event!
	$("#popupContactClose").click(function(){
		disablePopup();
	});
	//Click out event!
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});


	$('#s1').cycle({ 
    fx: 'custom', 
    cssBefore: {  
        top:  0, 
        left: 0, 
        width: 0, 
        height: 0,  
        zIndex: 1  
    }, 
    animIn:  {  
        width: 200, 
        height: 100  
    }, 
    animOut: {  
        top:  100, 
        left: 100, 
        width: 0, 
        height: 0 
    }, 
    cssAfter: {  
        zIndex: 0  
    } 
  });

  /*$('#s2').cycle({ 
    fx:     'scrollDown', 
    easing: 'bounceout', 
    delay:  -2000 
  });*/
  

 });

/*$(document).ready(function()
        {
              
            $('#layer1_form').ajaxForm({
                target: '#value_setup_content',
                success: function() 
                {
                    $("#layer1").hide();
                }                
            }); */   
				
			/*$("#inline").datepicker({ 
                 onSelect: function(date) { 
                     //alert("The chosen date is " + date); 
                 } 
            });
	    
            $("#login_cont").hide();
            
		    
            $('#reg_register').click(function()
            {
	            $("#login_cont").hide();
                $("#register_cont").show();
            });

	       $('#test').click(function()
            {
	           alert('tested!');
            });

            $('#log_register').click(function()
            {
	            $("#register_cont").hide();
                $("#login_cont").show();
            });
  });*/

  // apply inline-box only for mozilla
if( jQuery.browser.mozilla ) {
	// do when DOM is ready
	$( function() {
		// search form, hide it, search labels to modify, filter classes nocmx and error
		$( 'form.cmxform' ).hide().find( 'p>label:not(.nocmx):not(.error)' ).each( function() {
			var $this = $(this);
			var labelContent = $this.html();
			var labelWidth = document.defaultView.getComputedStyle( this, '' ).getPropertyValue( 'width' );
			// create block element with width of label
			var labelSpan = $("<span>")
				.css("display", "block")
				.width(labelWidth)
				.html(labelContent);
			// change display to mozilla specific inline-box
			$this.css("display", "-moz-inline-box")
				// remove children
				.empty()
				// add span element
				.append(labelSpan);
		// show form again
		}).end().show();
	});
};
   
   function processCommentForm() {
	   alert('ddd');return false;
	   $("#commentForm").validate();
       alert('ddd');return false;
   }

   function deleteBlog(id) {
	   var blLocation = 'http://clement.bubuface.com/blog/delete/' + id;
	    window.location = blLocation;
   }

   function toggleRegLog(obj) {
	   if(obj == 'log') {
		   var regDiv = getObj('register_cont');
		   var logDiv = getObj('login_cont');
		   /*regDiv.className='reg_tab_off';
		   logDiv.className='log_tab_on';
		   cssjs('remove',regDiv,'reg_tab_on');
		   cssjs('add',regDiv,'reg_tab_off');
		   cssjs('remove',logDiv,'log_tab_off');
		   cssjs('add',logDiv,'log_tab_on');*/
		   regDiv.style.display = "none";
		   logDiv.style.display = "block";
	   }

	   if(obj == 'reg') {
		   var regDiv = getObj('register_cont');
		   var logDiv = getObj('login_cont');
		   /*regDiv.className='reg_tab_on';
		   logDiv.className='log_tab_off';
		   cssjs('remove',regDiv,'reg_tab_off');
		   cssjs('add',regDiv,'reg_tab_on');
		   cssjs('remove',logDiv,'log_tab_on');
		   cssjs('add',logDiv,'log_tab_off');*/
		   logDiv.style.display = "none";
		   regDiv.style.display = "block";
	   }
   }

jQuery(document).ready(function($) {
  $('a[rel*=facebox]').facebox() 
}) 

function toggleTagValue(chkbox) {
	var newid = "tagHidden" + chkbox.value;
    //var obj = $("#"+newid);
	var obj = document.getElementById(newid);
	if(obj.value == "") {
		obj.value = chkbox.value;
		chkbox.checked = true;
	} else {
		obj.value = "";
		chkbox.checked = false;
	}
    /*if(chkbox.checked)
		 obj.value = chkbox.value;
	else
         obj.value = "";*/
    //chkbox.click();
	//alert(obj.value);
}

function submitTags(form) {
	//var obj = $("#comment-form");
	//alert("form is is: " + obj.id);
	//var currObj=getObj("mysql", "tagsForm");
	//alert("form is is: " + currObj.value);
    //alert(obj.mysql.value);
	var outp = "";

	/*$(':input', obj).each(function(){
		var type = this.type;
		outp = outp + ", " + type;
	});*/
	var elem = document.getElementById("tags-mysql").form;
	//alert(elem.soa.checked);
	/*if(elem.soa.checked == true) {
		alert("soa selected: " + elem.soa.value );
	} else {
		alert("soa not selected: " + elem.soa.value );
	}*/
	for(i=0; i<elem.elements.length; i++) {
		if(elem.elements[i].type == 'hidden') {
			if(strContains(elem.elements[i].name, "tagHidden"))
               //outp = outp + ", " + elem.elements[i].name + ": " + elem.elements[i].type + "; " + elem.elements[i].value;
			   if(elem.elements[i].value != "") {
				   if(outp != "")
				      outp = outp + ", ";
                   outp = outp + elem.elements[i].value;
			   }
		}
		/*if(strContains(elem.elements[i].name, "tagHidden")){
			   if(elem.elements[i].value != "") {
				   if(outp != "")
				      outp = outp + ", ";
                   outp = outp + elem.elements[i].value;
			   }
		}*/
	}
	document.getElementById("BlogTags").value = outp;
	//alert(outp);

	//alert(outp);
    jQuery(document).trigger('close.facebox');
	return false;
}
