/*if (dw_scrollObj.isSupported()) {
	dw_Event.add(window, 'load', dav);
}
*/

var cache = [];
// Arguments are image paths relative to the current page.
preLoadImages = function() {
	var args_len = arguments.length;
	for (var i = args_len; i--;) {
		var cacheImage = document.createElement('img');
		cacheImage.src = pathToImages + '/' + arguments[i];
		cacheImage.style.display = 'block';
		cacheImage.style.top = '1px';
		cacheImage.style.left = '1px';
		cacheImage.style.position = 'absolute';
//		cacheImage.style.display = 'block';
//		alert(cacheImage.src);
		cache.push(cacheImage);
	}
}

preLoadImages(
    'images/1.jpg','images/2.jpg','images/3.jpg','images/main.jpg',
	'images/leftdown.jpg','images/righttop.jpg',
	'images/lefttops1.jpg', 'images/lefttops2.jpg', 'images/rightdowns1.jpg', 'images/rightdowns2.jpg',
	'images/scrollbottom.png', 'images/scrollbottomon.jpg', 'images/scrollup.jpg', 'images/scrollupon.jpg',
	'images/scrollbarback.jpg',
	'tooltip/bl.png', 'tooltip/br.png', 'tooltip/tl.png', 'tooltip/bl.png', 'tooltip/andun.png',
	'images/scrollhandle.png', 'images/scrollhandle2.png'
	
)


init_dw_Scroll1 = function() {
	document.getElementById('scrollbar').style.height = document.getElementById('scrolling_parent1').offsetHeight + "px";
	var wndo1 = new dw_scrollObj('scrolling_parent1', 'scrolling1');
	wndo1.setUpScrollbar('dragBar', 'track', 'v', 1, 5, false);
}
init_dw_Scroll2 = function() {
	document.getElementById('scrollbar2').style.height = document.getElementById('scrolling_parent2').offsetHeight + "px";
	var wndo2 = new dw_scrollObj('scrolling_parent2', 'scrolling2');
	wndo2.setUpScrollbar('dragBar2', 'track2', 'v', 1, 5, false);
}

$(document).ready(function() { 
						   
	//$("#page1").hide();
	//$("#page2").hide();

	$(window).resize(function() {
		init_dw_Scroll1();
		init_dw_Scroll2();
	});
	$("#button1").click(function() {
		$("#page2").slideUp("", function() {
			$("#page1").slideToggle("", init_dw_Scroll1);
		});
	});
	
	$("#button1").hover( function() { showToolTip($("#button1"), "پست‌ها") }
	  , 
      	hideToolTip
    );
	
	$("#button2").hover( function() { showToolTip($("#button2"), "فتوبلاگ") }
	  , 
      	hideToolTip
    );
	
	$("#button3").hover( function() { showToolTip($("#button3"), "لینک‌ها") }
	  , 
      	hideToolTip
    );
	
	$("#button2").click(function() {
		$("#page1").slideUp("", function() {
			$("#page2").slideToggle("", init_dw_Scroll2);
		});
	});
	$("#page1Close").click(function() {
		$("#page1").slideUp("");
	});
	$("#page2Close").click(function() {
		$("#page2").slideUp("");
	});
	
	$("#button3").click(function() {
		if ($("#smallbox1").css("display") == "block" && 
			$("#smallbox2").css("display") == "block" &&
			$("#twitter").css("display") == "block")
		{
			$("#twitter").slideUp("fast", function(){
				$("#smallbox2").slideUp("fast", function(){
					$("#smallbox1").slideUp("fast")
				})
			});
		}
		else
		{
			$("#smallbox1").slideDown("fast", function(){
				$("#smallbox2").slideDown("fast", function(){
					$("#twitter").slideDown("fast");
				});
			});
		}
	});
});


var numberOfScrollingItems = 6;
var top = new Array(numberOfScrollingItems);
var shownPageItemId = 0;
for (h = 0; h < numberOfScrollingItems; h++)
	top[h] = 0;
	
var i;
function scrollUp(itemIndex) {
    var n = $("#scrolling" + itemIndex);

    if (n.offset().top + n.height() > n.parent().offset().top + n.parent().height()) {
        top[itemIndex] -= 5;
        n.css({'margin-top' : top[itemIndex] + "px" });
    }
    else
        stopScroll();
}

function scrollDown(itemIndex) {
    var n = $("#scrolling" + itemIndex);

    if (n.offset().top < n.parent().offset().top) {

        top[itemIndex] += 5;
        n.css({'margin-top' : top[itemIndex] + "px" });
    }
    else
        stopScroll();
}
function startScrollUp(itemIndex, t) {
    clearInterval(i);
    i = setInterval("scrollUp(" + itemIndex + ")", t);
}
function startScrollDown(itemIndex, t) {
    clearInterval(i);
    i = setInterval("scrollDown(" + itemIndex + ")", t);
}
function stopScroll() {
    clearInterval(i);
}


showToolTip = function(obj, message)
{
	var tooltipobj = $("#tooltip");
	
	$("#tooltipmain").html(message);

	tooltipobj.css({"left" : obj.offset().left + obj.width() - tooltipobj.width() - 25 + "px",
					"top" : obj.offset().top - tooltipobj.height() - 5 + "px"});
	
	$("#tooltip").delay(300).show(1);
}

hideToolTip = function()
{
	$("#tooltip").clearQueue();
	$("#tooltip").hide();
}

wpopen = function(macagna) {
	width = 350;
	height = 450;
    x = (screen.availWidth - width) / 2;
    y = (screen.availHeight - height) / 2;
	window.open(macagna, '_blank', 'width=' + width + ',height=' + height + ',scrollbars=yes,status=no,left='+x+',top='+y);
}
