$(init_page);

function init_page(){
  BrowserDetect.init();

  $(document).ready(function() {
    h2_adjust();
    h3_adjust();
	  margin_adjust();
	  if($.animate_bullets){
      $('ul.animate').animate_bullets();
    }
  });

  $(window).resize(function(){
    margin_adjust();
  });


  if($.fancybox !=undefined){
  	$("a.popup").fancybox({
      modal:true,
      padding:20,
      overlayShow:false,
      overlayOpacity:0.1,
      overlayColor:'#025',
      transitionIn:'fade'
  	});
  
    setTimeout(function(){
     $('a.popup').click();
    },1500);
  
    $('#popup_content a').click(function(){
      if(!$(this).hasClass('close')){  
        var page_id = $(this).parents('#popup_content').attr('page_id');
        clicked_link = this;
        close_popup( page_id, function(){go_through() });
        return false;
      }
    })
  }

}
function close_popup(page_id,callback){
  $.post(www_root + 'ajax/close_popup',{'page_id':page_id}, callback);
}

function go_through(){
  document.location = $(clicked_link).attr('href');
}
 
function h2_adjust(){
  $('.h2').each(function(idx){
    var width = $(this).width();
    if(width==0){
      this.onload = function(){
        width = this.width;
        var reps  = Math.ceil((width+52) / 52);
    	  if (reps > 11) reps=11;
    	  var new_width = reps * 52;
        var padding_right = new_width - width;
        $(this).css('padding-right', padding_right);
        $(this).parent().css('width', new_width);
      }
    }
    delegate_h2_adjust(this);
    var reps  = Math.ceil((width+52) / 52);
	  if (reps > 11) reps=11;
	  var new_width = reps * 52;
    var padding_right = new_width - width;
    $(this).css('padding-right', padding_right);
    $(this).parent().css('width', new_width);
	})
}
function delegate_h2_adjust(obj){
  setTimeout(function(){
    var width = $(obj).width();
    var reps  = Math.ceil((width+52) / 52);
    if (reps > 11) reps=11;
    var new_width = reps * 52;
    var padding_right = new_width - width;
    $(obj).css('padding-right', padding_right);
    $(obj).parent().css('width', new_width);
  },500);

            
}
function h3_adjust(){
  $('h3').each(function(idx){
    $(this).css('display','inline');
    var width = $(this).width();
    $(this).css('display','block');
    var reps  = Math.ceil((width+5) / 5);
	  var new_width = reps * 5;
    $(this).css('width',new_width);
  })
}

function margin_adjust(){
      var window_width = $(window).width();
      var margin = (window_width - 900 ) / 2;
	  //alert(window_width);

      $('#left_of_page').width(margin);
      $('#right_of_page').width(margin);
      $('#right_of_content').width(margin);
      $('#left_of_footer').width(margin);
      $('#right_of_footer').width(margin);
      $('#topdots').width(margin);
      $('#bottomdots').width(margin);

	  var left_pos = -347 + margin;
	  var position = left_pos+'px -115px'
	  $('#left_of_page').css('background-position', position );
	  var position = left_pos+'px 0px'
      $('#left_of_footer').css('background-position', position);
}

  
function replace_menu_items(){
    $('#main_menu a').each(function(idx){
      var href = $(this).attr('href');
      //alert(href);
      var text = $(this).html();
      var width =  $(this).width() ;
      img_src = www_root + 'template/novel/font_main_menu.php?size=14&text=' + text; 
      $(this).replaceWith('<a href="'+href+'"><img alt="' + text + '" src="' +  img_src  + '" /></a>');
      $(this).css('width',width);
      $(this).css('background-image','url('+ img_src  + ')');
      $(this).css('background-repeat','no-repeat');
      $(this).css('text-indent','10000px');
      })
}    


function is_valid_email(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(email) == false) {
      return false;
   }
   return true;
}

function setCookie(c_name,value,expiredays){
  var exdate=new Date();
  exdate.setDate(exdate.getDate()+expiredays);
  document.cookie=c_name+ "=" +escape(value)+
  ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

/* Browser Detection Class */
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]
};

