$(document).ready(function() {
	var preload = [];
	var img;
	if($('a.more').length) {
		/*img = new Image();
		img.src = '/images/new/more02.png';
		preload.push(img);*/
		preload.push('/images/new/more02.png');
	}

	if($('div.more2').length) {
		/*img = new Image();
		img.src = '/images/new/moreimages-act.png';
		preload.push(img);*/
		preload.push('/images/new/moreimages-act.png');
	}
	if ($('a.viewsite').length) {
		/*img = new Image();
		img.src = '/images/new/view-act.png';
		preload.push(img);*/
		preload.push('/images/new/view-act.png');
	}
	/*$('a.index_menu').each(function(){
		var src = this.getAttribute('bla');
		var src2 = src.substr(0, src.length -  5) + '2.png';
		preload.push(src2);
	});*/

	preload.push('/images/new/xmala/left_over.png');
	preload.push('/images/new/left-act.gif');
	preload.push('/images/new/xmala/left.png');
	preload.push('/images/new/left.gif');
	preload.push('/images/new/xmala/left_empty.png');
	preload.push('/images/new/xmala/left.png');
	preload.push('/images/new/xmala/arrow_up.png');
	preload.push('/images/new/xmala/right_empty.png');

	$('a.index_menu').each(function(){
		var img = this.getAttribute('bla');//this.style.backgroundImage.substr(4, this.style.backgroundImage.length - 5);
		//alert(this.style.backgroundImage);
		this.setAttribute('state_normal',  img);
		this.setAttribute('state_hover', img.substr(0, img.length -  5) + '2.png');
		preload.push(img.substr(0, img.length -  5) + '2.png');
		this.setAttribute('state_active', img.substr(0, img.length -  5) + '3.png');

		return;
		//alert(this.getAttribute('state_normal'));
		alert(this.getAttribute('state_active'));
		//alert(this.getAttribute('state_hover'));
	});


	var preload_images = [];
	for (var i = 0; i < preload.length; i++) {
		var img = new Image();
		img.src = preload[i];
		preload_images.push(img);
	}


	$('a').click(function(){
		this.blur();
	});

	$('a.index_menu').hover(
		function() {
			if ($(this).is('#main_' + cur_cat)) return;
			$(this).css('backgroundImage', 'url(' + this.getAttribute('state_hover') + ')');
		},
		function() {
			if ($(this).is('#main_' + cur_cat)) return;
			$(this).css('backgroundImage', 'url(' + this.getAttribute('state_normal') + ')');
		}
		/*function() {
			var img = this.getAttribute('bla');
			var img2 = img.substr(0, img.length -  5) + '2.png';
			$(this).css('backgroundImage', 'url(' + img2 + ')');
		},
		function() {
			var img = this.getAttribute('bla');
			$(this).css('backgroundImage', 'url(' + img + ')');
		}*/
	);
	//alert(cur_cat);
	$('#main_' + cur_cat).each(function(){
		//$(this).css('backgroundImage', 'url(' + this.getAttribute('state_active') + ')');
		//alert('url(' + this.getAttribute('state_active') + ')');
		this.style.backgroundImage = 'url(' + this.getAttribute('state_active') + ')';
	});


	$('a.viewsite').hover(
		function(){
			//alert(this.style.backgroundImage = '/images/new/more-hover.png');
			this.style.backgroundImage = 'url(/images/new/view-act.png)';
		},
		function(){
			this.style.backgroundImage = 'url(/images/new/view.png)';
		}
	);
	$('div.more2').hover(
		function(){
			//alert(this.style.backgroundImage = '/images/new/more-hover.png');
			this.style.backgroundImage = 'url(/images/new/moreimages-act.png)';
		},
		function(){
			this.style.backgroundImage = 'url(/images/new/moreimages.png)';
		}
	);


	/*$('a.purchase').hover(
		function(){
			$(this).find('div').css('top', '');
			//alert(this.style.backgroundImage = '/images/new/more-hover.png');
			this.style.backgroundImage = 'url(/images/new/more02.png)';
		},
		function(){
			this.style.backgroundImage = 'url(/images/new/more01.png)';
		}
	);*/

	$('#flash_vid').click(function(){
		window.open('/video_popup.html','', 'top=150, left=250, screenX=250, screenY=150, width=326,height=246, menubar=no, status=no, scrollbars=no, resizable=no, toolbar=no');
	});

	$('a.more').hover(
		function(){
			//alert(this.style.backgroundImage = '/images/new/more-hover.png');
			this.style.backgroundImage = 'url(/images/new/more02.png)';
		},
		function(){
			this.style.backgroundImage = 'url(/images/new/more01.png)';
		}
	);

	$('#promo_right').hover(
		function(){
			if (/*pageType == 'index'*/true) {
				var hover = '/images/new/xmala/arrow_over.png';
			} else {
				var hover = '/images/new/right-act.gif';
			}

			var img = $('#promo_right_img')[0];
			if ($(img).is('.active'))	img.src = hover;
		},
		function() {
			if (/*pageType == 'index'*/true) {
				var normal = '/images/new/xmala/arrow_up.png';
			} else {
				var normal = '/images/new/right.gif';
			}


			var img = $('#promo_right_img')[0];
			if ($(img).is('.active')) img.src = normal;
		}
	);
	$('#promo_left').hover(
		function(){
			if (/*pageType == 'index'*/true) {
				var hover = '/images/new/xmala/left_over.png';
			} else {
				var hover = '/images/new/left-act.gif';
			}

			var img = $('#promo_left_img')[0];
			if ($(img).is('.active'))	img.src = hover;
		},
		function() {
			if (/*pageType == 'index'*/true) {
				var normal = '/images/new/xmala/left.png';
			} else {
				var normal = '/images/new/left.gif';
			}


			var img = $('#promo_left_img')[0];
			if ($(img).is('.active'))	img.src = normal;
		}
	);




	if ( (pageType != 'supra') && (pageType != 'toasting')) {
		if (typeof page_images == 'undefined')	{
			page_images = [];
		}
		var images_main_nodes = $('a.link_thumb img');
		var images_main = [];
		images_main_nodes.each(function(){
			images_main.push(image_big(this.src));
		});
		var texts_main_nodes = $('td.handle_content');
		var texts_main = [];
		texts_main_nodes.each(function(){
			texts_main.push(this.innerHTML);
		});

		var tmp = [];
		/*for (var i = images_main.length - 1; i >= 0; i --) {
			tmp = new Array();
			tmp[0] = images_main[i];
			tmp[1] = texts_main[i];
			page_images.unshift(tmp);
		}*/
	}

	//console.dir(texts_main);
	//console.dir(page_images);
	//console.dir(texts_main);

	/*$('#header').hover(
		function(){
			setTimeout(
				function(){
					$('#search_box').css('backgroundImage', 'url(/images/search_bg2.jpg)');
				},
				200
			);
			$('#bgwhite').stopAll();
			$('#bgwhite').animate({top: 0}, 500);
		},
		function() {
			//this.stopAll;

			setTimeout(
				function() {
					$('#search_box').css('backgroundImage', 'url(/images/search_bg.jpg)');
				},
				200
			);

			$('#bgwhite').stopAll();
			$('#bgwhite').animate({top: -51, handle:'bgwhite_handle'}, 500);
		}
	);*/


	/*$('.menu_left a img').hover(
		function() {
			//alert(this.style.top)
			this.style.top = "-22px";
		},
		function() {
			this.style.top = "0px";
		}
	);*/

	$('#submit_button').hover (
		function() {
			//this.style.textDecoration = 'underline'	;
			//this.style.textDecoration = 'none';
			this.style.borderBottom = '1px dotted black';
			this.style.height = '18px';
			this.style.width  = '47px';
		},
		function() {
			//this.style.textDecoration = 'none';
			this.style.borderBottom = 'none';
			this.style.height = '18px';
			this.style.width  = '47px';

		}
	);




	promo_cur_pos = 0;
	promo_items = $('#promo_wrapper .promo_item').length;
	//alert(promo_items);
	$('#promo_left, #promo_right').css({visibility: 'visible'});
	//alert(promo_items);
	if (promo_items > 3) promo_right_enable();

	$('#promo_left').click(function() {
		if (promo_cur_pos == 0) {
			return;
		}

		promo_cur_pos ++;
		if (promo_cur_pos == 0) promo_left_disable();

		promo_right_enable();
		$('#promo_table').stopAll().animate({left: promo_cur_pos * 230}, 500);
	});

	$('#promo_right').click(function(){
		if (promo_cur_pos < 4 - promo_items) {
			return;
		}
		//alert('ok');
		promo_cur_pos --;
		if (promo_cur_pos < 4 - promo_items) promo_right_disable();

		promo_left_enable();
		$('#promo_table').stopAll().animate({left: promo_cur_pos * 230}, 500);
	});


	$('.handle_table').each(function(){
		this.handle_open = false;
		this.style.display = 'block';
	})

	/*$('.image_right').hover(
		function(){
			//alert(1);
			return;
			var handle = $(this).find('.handle_table');
			var left_dest = parseInt($(handle).find('.handle_content')[0].style.width);
			var samk = '/images/img_r_st_a.gif';
			handle.handle_open = ! handle.handle_open;
			$(handle).find('.handle_samk')[0].src = samk;
			$(handle).stopAll().animate({left: left_dest}, 500);
		},
		function() {
			//alert(2);
			return;

			var handle = $(handle).find('.handle_table');
			var left_dest = 0;
			var samk = '/images/img_r_st_b.gif';
			handle.handle_open = ! handle.handle_open;
			$(handle).find('.handle_samk')[0].src = samk;
			$(handle).stopAll().animate({left: left_dest}, 500);
		}
	);*/
/*
	$('.handle_table').click(function(){

		if (this.handle_open) {
			var left_dest = parseInt($(this).find('.handle_content')[0].style.width);
			var samk = '/images/img_r_st_a.gif';
		} else {
			var left_dest = 0;
			var samk = '/images/img_r_st_b.gif';
		}

		this.handle_open = ! this.handle_open;
		$(this).find('.handle_samk')[0].src = samk;
		$(this).stopAll().animate({left: left_dest}, 500);
	})
*/
	/*$('.handle_table').hover(
		function(){
			//alert(1);
			var left_dest = parseInt($(this).find('.handle_content')[0].style.width);
			var samk = '/images/img_r_st_a.gif';

			this.handle_open = ! this.handle_open;
			$(this).find('.handle_samk')[0].src = samk;
			$(this).stopAll().animate({left: left_dest}, 500);
		},
		function(){
			var left_dest = 0;
			var samk = '/images/img_r_st_b.gif';
			this.handle_open = ! this.handle_open;
			$(this).find('.handle_samk')[0].src = samk;
			$(this).stopAll().animate({left: left_dest}, 500);
		}
	);*/

/*
	var image_number = 0;
	$('a.link_thumb').each(function(){
		this.image_number = image_number;
		image_number ++;
	});
*/



/*
	$('a.link_thumb').click(function(){
		if (typeof topic_images != 'undefined') {
			page_images = topic_images[this.getAttribute('topic')];
			showPopunder(0);
		} else {
			showPopunder(this.image_number);
		}
		return false;
	});
*/
	var image_number = 0;
	$('table.image_right').each(function(){
		this.image_number = image_number;
		image_number ++;
	})
	.click(function(){
		if (typeof topic_images != 'undefined') {
			page_images = topic_images[this.getAttribute('topic')];
			showPopunder(0);
		} else {
			showPopunder(this.image_number);
		}
		return false;
	});




	$('#popunder_close').click(hidePopunder);
	$('#popunder_prev, #popunder_prev_text').click(popunder_prev);
	$('#popunder_next, #popunder_next_text').click(popunder_next);

	if ((typeof images_main !== 'undefined') && (page_images.length != images_main.length)) {
		$('.moreimages').css('display', 'block');
		$('.moreimages').click(function(){
			showPopunder(images_main.length);
		});
	}


	for (var i = 0; i < initFunctions.length; i++) {
		initFunctions[i]();
	}
});

function load() {
	for (var i = 0; i < loadFunctions.length; i++) {
		loadFunctions[i]();
	}

}

function promo_left_enable() {
	$('#promo_left').css({cursor: 'pointer'});
	if (/*pageType == 'index'*/true)	$('#promo_left_img')[0].src = '/images/new/xmala/left.png';
	$('#promo_left_img').addClass('active');
}
function promo_left_disable() {
	$('#promo_left').css({cursor: 'default'});

	if (/*pageType == 'index'*/true)	$('#promo_left_img')[0].src = '/images/new/xmala/left_empty.png';
	$('#promo_left_img').removeClass('active' );
}
function promo_right_enable() {
	$('#promo_right').css({cursor: 'pointer'});
	if (/*pageType == 'index'*/true)	$('#promo_right_img')[0].src = '/images/new/xmala/arrow_up.png';
	$('#promo_right_img').addClass('active');
}
function promo_right_disable() {
	$('#promo_right').css({cursor: 'default'});
	if (/*pageType == 'index'*/true)	$('#promo_right_img')[0].src = '/images/new/xmala/right_empty.png';
	$('#promo_right_img').removeClass('active');
}

function showPopunder(i) {
	//console.dir(page_images);
	popunder_showImage(i);
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();
	var top = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - 299) / 2);
	var left = (arrayPageSize[0] - 20 - 659) / 2;
	$('#popunder').css({
		display : 'block',
		top:  top,
		left: left
	});
}

function hidePopunder() {
	$('#popunder').css({
		display : 'none'
	});
}

function popunder_next() {
	/*if ($('#popunder')[0].image_cur == (page_images.length - 1) ) {
		var tmp = 0;
	} else {
		var tmp = parseInt($('#popunder')[0].image_cur) + 1;
	}*/
	var tmp = parseInt($('#popunder')[0].image_cur) + 1;


	popunder_showImage(tmp);
}
function popunder_prev() {
	/*if ($('#popunder')[0].image_cur == 0) {
		var tmp = page_images.length - 1;
	} else {
		var tmp = $('#popunder')[0].image_cur - 1;
	}*/
	var tmp = $('#popunder')[0].image_cur - 1;


	popunder_showImage(tmp);
}

function popunder_showImage(i) {
	//console.dir(page_images);
	$('#popunder_loading').css('display', 'block');
	$('#popunder_img')[0].onload = function() {
		$('#popunder_loading').css('display', 'none');
		//$('#popunder_img')[0].setAttribute('loaded', 'true');
	}


	
	$('#popunder_img')[0].src = page_images[i][0];
	$('#popunder_text')[0].innerHTML = page_images[i][1];

	$('#popunder')[0]['image_cur'] = i;
	if ($('#popunder')[0].image_cur == 0) {
		$('#popunder_prev, #popunder_prev_text').css({visibility: 'hidden'});
	} else {
		$('#popunder_prev, #popunder_prev_text').css({visibility: 'visible'});
	}

	if ($('#popunder')[0].image_cur == (page_images.length - 1) ) {
		$('#popunder_next, #popunder_next_text').css({visibility: 'hidden'});
	} else {
		$('#popunder_next, #popunder_next_text').css({visibility: 'visible'});
	}

	//$('#popunder_img')[0].setAttribute('loaded', 'false');
	/*setTimeout(
		function(){*/
			//if ($('#popunder_img')[0].getAttribute('loaded') == 'false') {

			//} 
		/*},
		20
	);*/
}

function getPageScroll() {
	var yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}
	arrayPageScroll = new Array('',yScroll)
	return arrayPageScroll;
}



//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){

	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
	return arrayPageSize;
}

function image_big(image) {
	var len = image.length;
	return image.substr(0, len - 4) + '_big.jpg';
}



function validateView()
{
	
	var errorMessage="";
	var isError=false;
	
	if (document.getElementById("viewTitle").value.toString()=="")
	{
		isError=true;
		errorMessage="No Title!\n";
	}
	
	if (document.getElementById("viewDescription").value.toString()=="")
	{
		isError=true;
		errorMessage+="No Content!\n";
	}
		
	
	for (var i=0; i<document.getElementById("form").elements.length; i++)
	{
		if (form.elements[i].type=="file")
		{
			if (form.elements[i].value.toString().length>0)
			{
				var x= new Array();
				x=form.elements[i].value.toString().split(".");
				
				if (x[x.length-1].toLowerCase()=="jpg" || x[x.length-1].toLowerCase()=="jpeg")
				{					
					var img = new Image();					
					img.src=form.elements[i].value.toString();													
				}
				else
				{
					isError=true;
					errorMessage+="Invalid Image Type!\n"; 
				}
			}
			
		}	
	}
	
	
	if (isError)
	{
		alert(errorMessage);
		return false;
	}
	
	return true;	
}


function addFileUpload()
{
	
	
	var n=0;
	for (var i=0; i<document.getElementById("form").elements.length; i++)
	{
		if (form.elements[i].type=="file")
		{
			n++;
		}	
	}
	
	
	if (n<=15)
	{
		
		var tr=document.createElement("tr");
		var td=document.createElement("td");		
		var file=document.createElement("input");
		file.type="file";
		file.name="viewFile*"+n; 
		file.id="viewFile*"+n;

		td.appendChild(file);		
		
			
		td.appendChild(file);		
		tr.appendChild(td);
		document.getElementById("filePlace").appendChild(tr);
		
		
		/*
		tr.insertCell(td);
		document.getElementById("filePlace").insertRow(tr);
		*/
		
		document.getElementById("view").style.height=(parseInt(document.getElementById("view").style.height)+25);
		document.getElementById("addviewTd").style.height=(parseInt(document.getElementById("addviewTd").style.height)+25);
		document.getElementById("addviewTable").style.height=(parseInt(document.getElementById("addviewTable").style.height)+25);
		document.getElementById("addviewInnerTd").style.height=(parseInt(document.getElementById("addviewInnerTd").style.height)+25);
		document.getElementById("addviewImage").style.height=(parseInt(document.getElementById("addviewImage").style.height)+25);
		document.getElementById("popunder_text").style.height=(parseInt(document.getElementById("popunder_text").style.height)+25);
		document.getElementById("mainTable").style.height=(parseInt(document.getElementById("mainTable").style.height)+25);
		document.getElementById("bottomTable").style.height=(parseInt(document.getElementById("bottomTable").style.height)+25);
		document.getElementById("bottomTd").style.height=(parseInt(document.getElementById("bottomTd").style.height)+25);
		document.getElementById("BottomImg").style.height=(parseInt(document.getElementById("BottomImg").style.height)+25);
		
		
	}
	else
	{
		alert("Time To Stop!");
	}
	
}

