jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

function waitingSign()
{
	$('#centralpage').html('<div style="width:620px;height:250px;"></div><div style="margin-left:280px;width:320px;height:250px;"><img src="images/load.gif"></div>');
}

function services()
{
	waitingSign();
	$('#centralpage').load("services.php");
}

function support()
{
	waitingSign();
	$('#centralpage').load("support.php");
}

function updateStatus()
{
	$.get('status.php', function(data) {
	  $('#statusbar').html(data);
	});
}

function updateUserMenu()
{
	$.post('userman.php?g=1', function(data) {
	  $('#bottommenu').html(data);
	});
}

function categories()
{
	waitingSign();
	$('#centralpage').load('categories.php');
}

function userManagement()
{
	waitingSign();
	$('#centralpage').load('userman.php');
}

function stats()
{
	waitingSign();
	$('#centralpage').load('stats.php');
}

function contactUs()
{
	waitingSign();
	$('#centralpage').load('contact.php');
}

function loadOrders()
{
	waitingSign();
	$('#centralpage').load('orders.php?all');
}

function loadAccounting()
{
	waitingSign();
	$('#centralpage').load('accounts.php');
}

function loadParams()
{
	waitingSign();
	$('#centralpage').load('settings.php');
}

function loadTranslations()
{
	waitingSign();
	$('#centralpage').load('tr.php');
}
function openProd(id)
{
	waitingSign();
	$('#centralpage').load('product.php?id=' + id);
}

function loadProducts()
{
	waitingSign();
	$('#centralpage').load('product.php');
}

function dropZoneInit(){

	$.fn.dropzone.uploadStarted = function(fileIndex, file){
		var infoDiv = $("<div></div>");
		infoDiv.attr("id", "dropzone-info" + fileIndex);
		infoDiv.html("upload started: " + file.fileName);
		
		var progressDiv = $("<div></div>");
		progressDiv.css({
			'background-color': 'orange',
			'height': '10px',
			'width': '0%',
			'font-size': '7pt'
		});
		progressDiv.attr("id", "dropzone-speed" + fileIndex);

		var fileDiv = $("<div></div>");
		fileDiv.addClass("dropzone-info");
		fileDiv.css({
			'border' : 'thin solid black',
			'margin' : '3px',
			'font-size': '7pt',
			'width': '150px'
		});
		fileDiv.append(infoDiv);				
		fileDiv.append(progressDiv);				
		
		$("#dropzone-info").after(fileDiv);
	};
	$.fn.dropzone.uploadFinished = function(fileIndex, file, duration){
		$("#dropzone-info" + fileIndex).html("uploaded " + file.fileName + " ("+getReadableFileSizeString(file.fileSize)+") in " + (getReadableDurationString(duration)));
		$("#dropzone-speed" + fileIndex).css({
			'width': '150px',
			'height' : '10px',
			'background-color': 'red',
			'font-size': '7pt'
		});
		if(uploadFinishedCallback!=null) uploadFinishedCallback(file.fileName);
	};
	$.fn.dropzone.fileUploadProgressUpdated = function(fileIndex, file, newProgress){
		$("#dropzone-speed" + fileIndex).css("width", newProgress + "%");
	};
	$.fn.dropzone.fileUploadSpeedUpdated = function(fileIndex, file, KBperSecond){
		var dive = $("#dropzone-speed" + fileIndex);

		dive.html( getReadableSpeedString(KBperSecond) );
	};
	$.fn.dropzone.newFilesDropped = function(){
		$(".dropzone-info").remove();
	};
	$("#dropzone").dropzone({
		url : "http://novorado.com/upload.php",
		printLogs : false,
		uploadRateRefreshTime : 500,
		numConcurrentUploads : 2
	});
	}
	
function loadEdtior() {
	tinyMCE.init({
		theme : "advanced",
		mode: "textareas",
		theme_advanced_toolbar_location : "top",
		theme_advanced_buttons1 : "bold,italic,"
		+ "removeformat,justifyfull,formatselect,"
		+ "bullist,code,link,unlink,image,",
		theme_advanced_buttons2 : "",
		theme_advanced_buttons3 : ""
		});
		
	dropZoneInit();
}

function addProduct()
{
	waitingSign();
	$('#centralpage').load('product.php?c=add');
}

function listProducts()
{
	waitingSign();	
	$('#centralpage').load('product.php');
}

function editProductPics(id)
{
	waitingSign();
	$('#centralpage').load('prodimage.php?id=images:'+id);
}

function editProduct(id)
{
	waitingSign();
	$('#centralpage').load('product.php?edit='+id);
}

function deleteProduct(id)
{
	waitingSign();
	$('#centralpage').load('product.php?delete='+id);
}

function loadRussian()
{
	location.href="/?lang=ru";

	/*
	waitingSign();
	$('#lang').load('setlang.php?l=ru');
	$('#lang').fadeOut(1000).delay(500).fadeIn(1000);
	$('#lang').ready(function() {
		location.reload();
		});
	*/
}

function loadEnglish()
{
	location.href="/?lang=en";
	/*
	waitingSign();
	$('#lang').load('setlang.php?l=en');
	$('#lang').fadeOut(1000).delay(500).fadeIn(1000);
	$('#lang').ready(function() {
		location.reload();
		});
	*/
}

function loadSecurityLog()
{
	waitingSign();
	$('#centralpage').load('security.php');	
}

function JIPECatSetup()
{
  $(".dblclick").editable("jep/catsave.php", { 
      indicator : "<img src='images/indicator.gif'>",
      tooltip   : "Click to edit",
      style  : "inherit"
  });
}

function JIPESetup()
{
	
$("a[rel^='prettyPhoto']").prettyPhoto();

 $('.prodimg').bind('click', function(event){
	$('#centralpage').load("prodimage.php?id="+event.target.id);
	});
	
  $(".editable_select").editable("jep/jepsave.php", { 
    indicator : '<img src="images/indicator.gif">',
    data   : "{'Lorem ipsum':'Lorem ipsum','Ipsum dolor':'Ipsum dolor','Dolor sit':'Dolor sit'}",
    type   : "select",
    submit : "OK",
    style  : "inherit",
    submitdata : function() {
      return {id : 2};
    }
  });
  $(".editable_select_json").editable("jep/jepsave.php", { 
    indicator : '<img src="images/indicator.gif">',
    loadurl : "jepjson.php",
    type   : "select",
    submit : "OK",
    style  : "inherit"
  });
  $(".editable_textarea").editable("jep/jepsave.php", { 
      indicator : "<img src='images/indicator.gif'>",
      type   : 'textarea',
      submitdata: { _method: "put" },
      select : true,
      submit : 'OK',
      cancel : 'cancel',
      cssclass : "editable"
  });
  $(".editable_textile").editable("jep/jepsave.php?renderer=textile", { 
      indicator : "<img src='images/indicator.gif'>",
      loadurl   : "jepload.php",
      type      : "textarea",
      submit    : "OK",
      cancel    : "Cancel",
      tooltip   : "Click to edit..."
  });
  
  $(".click").editable("jep/jepsave.php", { 
      indicator : "<img src='images/indicator.gif'>",
      tooltip   : "Click to edit",
      style  : "inherit"
  });
  $(".dblclick").editable("jep/jepsave.php", { 
      indicator : "<img src='images/indicator.gif'>",
      tooltip   : "Doubleclick to edit",
      event     : "dblclick",
      style  : "inherit"
  });
  $(".mouseover").editable("jep/jepecho.php", { 
      indicator : "<img src='images/indicator.gif'>",
      tooltip   : "Move mouseover to edit...",
      event     : "mouseover",
      style  : "inherit"
  });
  
  /* Should not cause error. */
  $("#nosuch").editable("jep/jepecho.php", { 
      indicator : "<img src='images/indicator.gif'>",
      type   : 'textarea',
      submit : 'OK'
  });
}

function openCat(cat){
	waitingSign();
	$('#centralpage').load("categories.php?cat=" + cat);
	}
	
function shoppingCart()
{
	waitingSign();
	
	// Check against the database. Quantity might have changed while we were shoppin
	$.post('cart.php',{vercart:$.cookie('novocart')},function(newcart){
		$.cookie('novocart',newcart,{expires: 3});
			
		if($.cookie('novocart')==null){
			openCat('ABBA');
			$('#headercart').hide('2000');
		} else {		
			$.post("cart.php", { cart: $.cookie('novocart'), cartopt:  $.cookie('shipoptions')}, function(data) {
				$('#centralpage').html(data);
				});
			 }
		});
}

function geoCoderSuccess(addr){
	$.post("server.php", { address: addr });
	}
	
function geoCoderFail(status){
	$.post("server.php", { addressfail: 1 });
	}
	
function refreshNovocartItems(){

	if($.cookie('novocart')==null) return 0;

	// Check against the database. Quantity might have changed while we were shoppin
	$.post('cart.php',{vercart:$.cookie('novocart')},function(newcart){
		$.cookie('novocart',newcart,{expires: 3});
	
		c=$.cookie('novocart');
		if(c==null) return 0;
		arr=c.split(" ");
		sz=0;
		for(i=0;i<arr.length;i++){
			if(arr[i]==null) continue;
			a2=arr[i].split(':');
			if(a2[1]!=null) sz+=a2[1]*1;
			}
			
		if(sz==0){
			$('#headercart').hide('fast');
			} else {
				$('#headercart').html(sz);
				if(sz<10) $('#headercart').css('width','60px');
				else $('#headercart').css('width','90px');
				$('#headercart').show(2000);
				}
		});
	}
	
function wideScreen(){
	$(".container").css("margin","10px");
	$(".container").css("width",$(window).width()-20);
	$(".ccentral").css("height",$(window).height()-120);
	
	$(".outerpair1").css("width",$(window).width()-20);
	
	$(".shadowbox").css("height","100%");
	$(".innerbox").css("height","100%");
	
	// header and footer
	$(".central").css("width","100%");
	$(".header").css("width","100%");
	$(".cfooter").css("width","100%");
	
	/*
	$(".outerpart1").css("width",$(".ccentral").css("width")-4);
	$(".outerpart2").css("width",$(".outerpart1").css("width")-4);
	$(".shadowbox").css("width",$(".outerpart2").css("width")-4);
	$(".innerbox").css("width",$(".shadowbox").css("width")-4);
	$(".central").css("width",$(".innerbox").css("width")-4);
	*/
	
	$("#centralpage").css("width","100%");
	$("#centralpage").css("height","100%");
	$(".footer").css("width","100%");
	$("#bottommenu").css("width","100%");
	}
