var CH;     //true height of the content 
var CW;    //true width of the content
var BW;    //visible width of the content
var BH;     //visible height of the content

var scrLength=120;   //The length of the scroller's lines
var stepLength=30;

$(document).ready(function () {
	$("a[rel^='prettyPhoto']").prettyPhoto();
	
	if(document.getElementById("c_srolling"))
	{
					BH=$("#cc_1").height();
					BW=$("#cc_1").width();
					CH=$("#cc_2").height();
					CW=$("#cc_2").width();
										
					SH=Math.floor(scrLength*BH/CH);
					SW=Math.floor(scrLength*BW/CW);
					
					$("#vscroll").css("height", SH+"px");
					$("#hscroll").css("width", SW+"px");
					
					if(BH<CH)
					{
							$(".scroller_vrule").css("visibility","visible");
							
							//Делаем скроллер перетаскиваемым
							$("#vscroll").draggable({ axis: 'y', containment: 'parent', 
							start: function(event, ui) { },
							drag: function(event, ui) { 
								contentTop=-Math.ceil(ui.position.top*CH/scrLength);
								$("#cc_2").css("top", contentTop+"px");
							},
							stop: function(event, ui) {	}
							});
					}
					
					if(BW<CW)
					{
							$(".scroller_hrule").css("visibility","visible");
							
							//Делаем скроллер перетаскиваемым
							$("#hscroll").draggable({ axis: 'x', containment: 'parent', 
							start: function(event, ui) { },
							drag: function(event, ui) { 
								contentLeft=-Math.ceil(ui.position.left*CW/scrLength);
								$("#cc_2").css("left", contentLeft+"px");
							},
							stop: function(event, ui) { }
							});
					}
				
					//adding the event listerner for Mozilla
					if(window.addEventListener)
						document.addEventListener('DOMMouseScroll', moveObject, false);
					//for IE/OPERA etc
					document.onmousewheel = moveObject;
	}
	
});



function stepper(dir)
{
	multiplier=30;
	if(dir==1 || dir==3)
	 {
			curstep=multiplier*CH/BH;
	 }
	 else 
	 {
		 	curstep=multiplier*CW/BW;
	 }
	
	curstep=Math.ceil(curstep);
	
	switch(dir)
	{
		case 1: step=parseIntPx($("#cc_2").css("top"))+curstep; 
					if(step<curstep && step>(-CH+BH-stepLength))
					{
						 $("#cc_2").animate({ 
       						top: step+"px"
      					}, 500 );					 
					}
					break;
					
		case 2: step=parseIntPx($("#cc_2").css("left"))-curstep; 
					if(step<curstep && step>(-CW+BW-stepLength))
					{
						$("#cc_2").animate({ 
       						left: step+"px"
      					}, 500 );	
					}
					break;
					
		case 3: step=parseIntPx($("#cc_2").css("top"))-curstep; 
					if(step<curstep && step>(-CH+BH-stepLength))
					{
						$("#cc_2").animate({ 
       						top: step+"px"
      					}, 500 );	 
					}
					break;
					
		case 4:  step=parseIntPx($("#cc_2").css("left"))+curstep; 
					if(step<curstep && step>(-CW+BW-stepLength))
					{
						$("#cc_2").animate({ 
       						left: step+"px"
      					}, 500 );	 
					}
					break;
	}	
	recalcVertical();
	recalcHorisontal();
}



function makeStep(dir)
{
	step=dir=="up"?-stepLength:stepLength; 
	
	if(BH<CH)
	 {
		 	step+=parseIntPx($("#cc_2").css("top"));
			if(step<stepLength && step>(-CH+BH-stepLength))
			{
				$("#cc_2").css("top", step+"px");
				recalcVertical();
			}
	 }
	 else if(BW<CW)
	 {
		 	step+=parseIntPx($("#cc_2").css("left"));
			if(step<stepLength && step>(-CW+BW-stepLength))
			{
				$("#cc_2").css("left", step+"px");
				recalcHorisontal();
			}
	 }
}



function parseIntPx(str)
{
	str=parseInt(str);
	return str;	
}


function recalcVertical()
{
	 tops=-Math.ceil(scrLength*parseIntPx($("#cc_2").css("top"))/CH);
	 if((tops+$("#vscroll").height())<=scrLength && tops>=0)
	 {
		$("#vscroll").css("top",tops+"px");
	 }
}

function recalcHorisontal()
{
	//window.status=parseIntPx($("#cc_2").css("left"));
	lefts=-Math.ceil(scrLength*parseIntPx($("#cc_2").css("left"))/CW);
	 if((lefts+$("#hscroll").width())<=scrLength && lefts>=0)
	 {
		$("#hscroll").css("left",lefts+"px");
	 }
}


function moveObject(event)
{
   
    var delta = 0; 
    if (!event) event = window.event; 
    // normalize the delta
    if (event.wheelDelta) { 
        // IE and Opera
        delta = event.wheelDelta ; 
    } else if (event.detail) { 
        // W3C
        delta = -event.detail ;
    }
     
	if(delta>0)
	{
		makeStep("down");
	}
	else
	{
		makeStep("up")
	}	
}





//-----------------------initialization of lists
var dropDownStates=new Array();

function listsInit()
{
	$(".dropdownText").each(
		function (i) 
		{
			//$(this).wrap("<div class='dropdownText' id='ddText_"+i+"'></div>");		
			$(this).html("<div class='dropdownText' id='ddText_"+i+"'>"+$(this).html()+"</div>");
			$("#ddText_"+i).hide();
		}
	);
	
	
	$(".dropdownCaption").each(
		function (i) 
		{
			dropDownStates[i]=0;
			$(this).attr("id","ddCaption_"+i);
			$(this).click(function(){
				hlDropDown(i, "ddCaption_"+i, "ddText_"+i)
			});			
		}
	);	
}
	
function hlDropDown(i, id, textId)
{
	if(dropDownStates[i])
	{
		//$("#"+textId).hide();		
		//$("#"+textId).slideUp("slow");
		//$("#"+textId).fadeOut("slow");
		$("#"+id).attr("class", "dropdownCaption");
	}
	else
	{
		//$("#"+textId).show();
		//$("#"+textId).slideDown("slow");
		//$("#"+textId).fadeIn("slow");	
		$("#"+id).attr("class", "dropdownCaptionActive");		
	}
	$("#"+textId).slideToggle("slow");	
	dropDownStates[i]=1-dropDownStates[i];
}



	
//----------------PNG FIX---------------------
function bgFixPng() {
	if(is_ie6)
	{			
		$("div").each(function () {
			obgFixPng($(this));
		});		
	}
}

function obgFixPng(obj)
{		
	obj=$("#"+obj);	
	if(is_ie6)
	{
		var bg  = obj.css("background-image");				
		if(bg)
		{		
			if(bg.length)
			{
				var src = bg.substring(5, bg.length-2);
				var sizingMethod = (obj.css("background-repeat") == "no-repeat") ? "crop" : "scale";
				obj.css("filter", "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='" + sizingMethod + "')");
				obj.css("background-image","url("+base_url+"public/css/pngfix.gif)");	
			}
		}		
	}	
	
}
//----------------------------------------	

var colors=new Array();
	
//Функция подсветки пунктов меню
function mainmenu_hl(id, state)
{
	menuItem=$("#mainmenudiv_"+id);
	linkItme=$("#mainmenulink_"+id);
	sm=$("#sm_"+id);
	u=$("#u_"+id);
	menuClass=menuItem.attr("class");	
	
	if(menuClass!="active_menu_container")
	{
		if(state)
		{
			colors[id]=linkItme.css("color");
			menuItem.attr("class","highlighted_menu_container");
			linkItme.css("color","#fff");
			u.css("visibility","visible");
			sm.css("display","block");
			//sm.fadeIn("fast");
			//menuItem.css("background-color","#033");
		}
		else
		{
			menuItem.attr("class","passive_menu_container");
			linkItme.css("color",colors[id]);
			u.css("visibility","hidden");
			sm.css("display","none");
			//sm.fadeOut("fast");
			//menuItem.css("background-color","#fff");
		}
	}
}
