var pagify = function(div,heigth){
	paginator = new Paginator(div,heigth);
	paginator.init();
}

var pagify2 = function(div,heigth){
	paginator = new Paginator2(div,heigth);
	paginator.init();
}

var Paginator = function(div,page_height){
	this.div = jQuery(div)[0];
	var newdiv = document.createElement("div");
	newdiv.innerHTML = '<div class="myinline">'+this.div.innerHTML+'</div>';
	this.div.innerHTML = newdiv.innerHTML;

	this.content_div = this.div.firstChild;
	
	this.page = new Object();
	this.pages = new Object();
	this.content = new Object();
	this.page.height = page_height;
}

Paginator.prototype.init = function(){
	this.paginateBlocks();
	this.createPages();

}

Paginator.prototype.paginateBlocks = function(){
	elements = this.content_div.children;
	this.content.height = this.page.height-20;
	for(i in elements){
		if(!elements[i] || i=="length") return;
		if(jQuery.browser.msie){
			if(parseInt((elements[i].offsetTop-390)/this.content.height) != parseInt(((elements[i].offsetTop-390) + elements[i].offsetHeight+10)/this.content.height)){
				margin = this.content.height-((elements[i].offsetTop-390)%this.content.height)+25;
				jQuery(elements[i]).attr('style','margin-top:'+margin+'px;');
			}
			
		} else {
			if(parseInt((elements[i].offsetTop-390)/this.content.height) != parseInt(((elements[i].offsetTop-390) + elements[i].offsetHeight+10)/this.content.height)){
				margin = this.content.height-((elements[i].offsetTop-390)%this.content.height)+25;
				jQuery(elements[i]).attr('style','margin-top:'+margin+'px;');
			}
		}
	}
}

Paginator.prototype.createPages = function(){
	if(jQuery.browser.msie) this.clientHeight = this.div.offsetHeight;
	else this.clientHeight = this.div.clientHeight;
	if(this.clientHeight<=this.page.height) return false;
	this.content.height = this.page.height-20;
	this.pages.count = parseInt(this.clientHeight/(this.page.height-20));//Переписати
	jQuery(this.div).attr('style','height:'+(this.page.height-20)+'px;');//20 вводити в характеристиках
	if(jQuery.browser.msie)	jQuery(this.div).attr('style',jQuery(this.div).attr('style')+';overflow:hidden;'); 
	else jQuery(this.div).attr('style',jQuery(this.div).attr('style')+'overflow:hidden;'); 

	this.div.parentNode.innerHTML+=this.drawNavigation();//переписати
	this.cursor = 0;
	jQuery('.prev').attr('style','visibility:hidden;');	
}

Paginator.prototype.drawNavigation = function(){
	html = "<div class='pag_navigator'><div class='prev' onclick='paginator.prev();'>&nbsp;</div><div class='next' onclick='paginator.next();'>&nbsp;</div></div>";//переписати
	return html;
}

Paginator.prototype.checkButtons = function(){
	if(this.cursor==this.pages.count) jQuery('.next').attr('style','visibility:hidden;');
	else jQuery('.next').attr('style','visibility:visible;');


	if(this.cursor==0) jQuery('.prev').attr('style','visibility:hidden;');
	else jQuery('.prev').attr('style','visibility:visible;');
}

Paginator.prototype.next = function(){
	this.cursor++;
	if(this.cursor>this.pages.count){
		this.cursor = 0;
	}
	this.checkButtons();
	this.slideToCurrentPage();
}

Paginator.prototype.prev = function(){
	this.cursor--;
	if(this.cursor<0){
		this.cursor = this.pages.count;
	} 
	this.checkButtons();
	this.slideToCurrentPage();
}

Paginator.prototype.slideToCurrentPage = function(){
	poffset = this.cursor * this.content.height;
	jQuery('.myinline').attr('style','margin-top:-'+poffset+'px;'); 
}
Paginator2 = Paginator;
Paginator2.prototype.paginateBlocks = function(){
	elements = this.content_div.children;
	this.content.height = this.page.height-20;
	for(i in elements){
		if(!elements[i] || i=="length") return;
		if(jQuery.browser.msie){
			if(parseInt((elements[i].offsetTop-152)/this.content.height) != parseInt(((elements[i].offsetTop-152) + elements[i].offsetHeight+10)/this.content.height)){
				margin = this.content.height-((elements[i].offsetTop-152)%this.content.height)+25;
				jQuery(elements[i]).attr('style','margin-top:'+margin+'px;');
			}
			
		} else {
			if(parseInt((elements[i].offsetTop-152)/this.content.height) != parseInt(((elements[i].offsetTop-152) + elements[i].offsetHeight+10)/this.content.height)){
				margin = this.content.height-((elements[i].offsetTop-152)%this.content.height)+25;
				jQuery(elements[i]).attr('style','margin-top:'+margin+'px;');
			}
		}
	}
}



jQuery(document).ready(function(){		
		form = document.getElementById("mailform");
    selectEl = function(){
    	this.select();
    }    
if(form){
    form.name.onfocus = selectEl;
    form.email.onfocus = selectEl;
    form.phone.onfocus = selectEl;
    form.message.onfocus = selectEl;
}
});

jQuery(document).ready(function(){
		/*if(jQuery('#c199 .csc-textpic-text').length!=0)
			pagify("#c199 .csc-textpic-text",250);

		if(jQuery('#c188 .csc-textpic-text').length!=0)
			pagify2("#c188 .csc-textpic-text",430);*/

		if(jQuery('.pag_navigator')){
			L=getUrlVars()['L'];
			if(!L)L=0;
			pageid = getUrlVars()['id'];
			if(jQuery('.pag_navigator .prev')){
				prevpid = jQuery('.pag_navigator .prev').attr('pageid');
			 	jQuery('.pag_navigator .prev').bind('click',function(){window.location='index.php?id='+prevpid+'&L='+L});
			}
			if(jQuery('.pag_navigator .next')){
				nextpid = jQuery('.pag_navigator .next').attr('pageid');
				jQuery('.pag_navigator .next').bind('click',function(){window.location='index.php?id='+nextpid+'&L='+L});
			}
			
		}


});

function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

