var intval = 0;
var intval2 = 0;

function mainFormInit() {
	initComponents();
}

function initComponents() {
	/* activate animation */
	/* activa slideshow */
	$(function() {
	    setInterval("slideSwitch()", 6000);
	});
}

function setActiveMenu(option) {
/*	if (option === "" || option === "undefined") {
		option = "home";
	}*/
	$("#"+option).addClass("selected");
}


// mudar parar tirar url da 
// mudar para por dinamico o tamanho (calcular %%)
//function showImage(url, title) {
function showImage(id) {
	var url = $("#"+id).attr("url");
	var title = $("#"+id).attr("tooltip");
	
	var size = getImageViewerNewSize("#buscaModal", ".imageWrapper");
	
	$("#buscaModalIMG").attr('src', url);
	$("#buscaModalIMG").attr('title', title);
	$("#buscaModalIMG").attr('alt', title);
	
	//alert(size[0] + " - " + size[1] + " - " + size[2] + " - " + size[3]);
	$("#buscaModal").width(size[0] + "px");
	$("#buscaModal").height(size[1] + "px");
	$(".imageWrapper").css("line-height", size[1] + "px");
	//doBlockUI("buscaModal", 720, 480, 7, 25);
	doBlockUI("buscaModal", size[0], size[1], size[2], size[3]); //.blockPage
	
}

function showImagePostal(id) {
	var url = $("#"+id).attr("url");
	var title = $("#"+id).attr("tooltip");
	
	var size = getImageViewerNewSizePostal("#buscaModal", ".imageWrapper");
	
	$("#buscaModalIMG").attr('src', url);
	$("#buscaModalIMG").attr('title', title);
	$("#buscaModalIMG").attr('alt', title);
	
	//alert(size[0] + " - " + size[1] + " - " + size[2] + " - " + size[3]);
	$("#buscaModal").width("650px");
	$("#buscaModal").height("460px");
	$(".imageWrapper").css("line-height", "460px");
	//doBlockUI("buscaModal", 720, 480, 7, 25);
	doBlockUI("buscaModal", "650px", "460px", size[2], size[3]); //.blockPage
	
}


function closeImage() {
	$.unblockUI();
	$("#buscaModalIMG").attr('src', '');
	$("#buscaModalIMG").attr('title', '');
	$("#buscaModalIMG").attr('alt', '');
}

function doBlockUI(div, bWidth, bHeight, bTop, bLeft) {
//	$.blockUI({ message: $("#buscaModal"), css: { width: '576px', height:'430px', top: '15%', left: '30%' } }); 
	$.blockUI({ message: $("#"+div), 
		css: { 	width: bWidth+'px', 
				//height: bHeight+'px', 
				top: bTop+'%', 
				left: bLeft+'%'
			}});
}

function getImageViewerNewSize(divID, childID) {
	var size = new Array(720, 480, 7, 25);
	var marginH = 160;
	var marginV = 120;
	var minW = parseInt($(divID).css("min-width").replace("px", ""));
	var maxW = parseInt($(divID).css("max-width").replace("px", ""));
	var minH = parseInt($(divID).css("min-height").replace("px", ""));
	var maxH = parseInt($(divID).css("max-height").replace("px", ""));
	var viewportWidth = parseInt($(window).width());
	var viewportHeight = parseInt($(window).height());
	
	var width = viewportWidth - marginH;
	if (width > maxW) {
		width = maxW;
	} else if (width < minW) {
		width = minW;
	}
	
	var height = viewportHeight - marginV;
	if (height > maxH) {
		height = maxH;
	} else if (height < minH) {
		height = minH;
	}
	
	var percW = parseInt((((viewportWidth - width) / 2) / viewportWidth) * 100);
	var percH = parseInt((((viewportHeight - height) / 2) / viewportHeight) * 100);
	
	size[0] = width;
	size[1] = height;
	size[2] = percH;
	size[3] = percW;

	return size;
}

function getImageViewerNewSizePostal(divID, childID) {
	var size = new Array(670, 480, 7, 25);
	var marginH = 160;
	var marginV = 120;
	var minW = 650;
	var maxW = 650;
	var minH = 460;
	var maxH = 460;
	var viewportWidth = parseInt($(window).width());
	var viewportHeight = parseInt($(window).height());
	
	var width = viewportWidth - marginH;
	if (width > maxW) {
		width = maxW;
	} else if (width < minW) {
		width = minW;
	}
	
	var height = viewportHeight - marginV;
	if (height > maxH) {
		height = maxH;
	} else if (height < minH) {
		height = minH;
	}
	
	var percW = parseInt((((viewportWidth - width) / 2) / viewportWidth) * 100);
	var percH = parseInt((((viewportHeight - height) / 2) / viewportHeight) * 100);
	
	size[0] = width;
	size[1] = height;
	size[2] = percH;
	size[3] = percW;

	return size;
}

function resizeImageViewer(divID, childID) {
	$(window).resize(function() {
		var size = getImageViewerNewSize(divID, childID);
		$(divID).width(size[0] + "px");
		$(divID).height(size[1] + "px");
		$(childID).css("line-height", size[1] + "px");
		$(".blockPage").width(size[0] + "px");
		$(".blockPage").height(size[1] + "px");
		$(".blockPage").css("top", size[2] + "%");
		$(".blockPage").css("left", size[3] + "%");
	});
}

function slideSwitch() {
    var $active = $('#slideshow div.active');

	if ( $active.length == 0 ) $active = $('#slideshow div:last');

    // use this to pull the images in the order they appear in the markup
    var $next = $active.next().length ? $active.next()
        : $('#slideshow div:first');

    // uncomment the 3 lines below to pull the images in random order    
    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );
	
    $active.addClass('last-active');

	$next.css({opacity: 0.0})
        .addClass('active')
		.removeClass('hide')
        .animate({opacity: 1.0}, 1000, function() {
			$active.removeClass('active last-active');
			$active.addClass('hide');
		});
}

/* Scroll */

function divScroll(id, dir) {
//	$("#"+id).scrollTop($("#"+id).scrollTop() + dir);
	intval = setInterval(function (e) {
		divScrollInterval(id, dir);
	},50);
}

function divScroll2(id, dir) {
//	$("#"+id).scrollTop($("#"+id).scrollTop() + dir);
	intval2 = setInterval(function (e) {
		divScrollInterval(id, dir);
	},50);
}

function divScrollInterval(id, dir) {
	$("#"+id).scrollTop($("#"+id).scrollTop() + dir);
}

function cancelScroll() {
	clearInterval(intval);
}
function cancelScroll2() {
	clearInterval(intval2);
}

function scrollInit(div, n) {
	$("#scrollBottom"+n).mouseover(function (e) {
		divScroll(div, 8);
	});
	$("#scrollBottom"+n).mouseout(cancelScroll);
	$("#scrollTop"+n).mouseover(function (e) {
		divScroll(div, -8);
	});
	$("#scrollTop"+n).mouseout(cancelScroll);
	
	$("#"+div).mousewheel(function(e, delta){
		var speed = 12;
		var sliderVal = -1 * (delta*speed);//increment the current value
	
		divScrollInterval(div, sliderVal);
	
//		console.log("Delta: "+delta+" - Slider Value: "+sliderVal);
//		return false;
		e.preventDefault();//stop any default behaviour
	});
}

function scrollInit2(div, n) {
	$("#scrollBottom"+n).mouseover(function (e) {
		divScroll2(div, 8);
	});
	$("#scrollBottom"+n).mouseout(cancelScroll2);
	$("#scrollTop"+n).mouseover(function (e) {
		divScroll2(div, -8);
	});
	$("#scrollTop"+n).mouseout(cancelScroll2);
	
	$("#"+div).mousewheel(function(e, delta){
		var speed = 12;
		var sliderVal = -1 * (delta*speed);//increment the current value
	
		divScrollInterval(div, sliderVal);
	
//		console.log("Delta: "+delta+" - Slider Value: "+sliderVal);
//		return false;
		e.preventDefault();//stop any default behaviour
	});
}

function legalFormInit() {
	scrollInit('legaltopcontentText', '');
}

function contactFormInit() {
	scrollInit('contacttopcontentText', '');
}

function newsFormInit() {
	scrollInit('newstopcontentText', '');
}

function newsDetailFormInit() {
	scrollInit('newsdetailtopcontentText', '');
}

function concertsFormInit() {
	scrollInit('concertstopcontentText', '');
}
function artistsFormInit() {
	scrollInit('artiststopcontentText', '');
}
function artistDetailFormInit() {
	scrollInit('artistDetailBody', '');
	scrollInit2('artistDetailRecordBody', '2');
}
function pressFormInit() {
	scrollInit('presstopcontentText', '');
}
function storeFormInit() {
	scrollInit('storetopcontentText', '');
}

function genericFormInit(id, n) {
	scrollInit(id, n);
}
