$(document).ready(function(){
	minWidth();
	
	if( $(".adviceDancer").is("div") ){
		jQuery(".adviceDancer #carousel").jcarousel({
			scrollAnimation : 800,
			noButtons : false
		});
	}
	
	if( $(".advice").is("div") ){
		jQuery(".advice #carousel").jcarousel({
			itemVisible : 1,
			itemScroll : 1,
			scrollAnimation : 800,
			noButtons : false
		});
	}
	
	/*$(window).bind("load",function(){
		alert("dd");
		fixSample();
		centerBlocksPress();
		initScroller();
	});
	$(window).bind("resize",function(){
		alert("dd2");
		minWidth();
	});*/
});

addLoadEvent(initScroller);
addLoadEvent(fixSample);
addLoadEvent(centerBlocksPress);
addResizeEvent(minWidth);

function centerBlocksPress(){ // press : center blocks
	var totalWidth = 0;
	var container = $(".press .container").width();
	$(".press .pressItem").each(function(){
		totalWidth += $(this).width() + 51;
	});
	marginLeft = Math.round((container-(totalWidth-51))/2);
	$(".press .pressItem:first").css("marginLeft",marginLeft + "px");
}

function addLoadEvent(func) { // Manage Load Event
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}

function addResizeEvent(func) { // Manage Resize Event
    var oldonresize = window.onresize;
    if (typeof window.onresize != 'function') {
        window.onresize = func;
    } else {
        window.onresize = function() {
            oldonresize();
            func();
        }
    }
}

function getWindowHeight() {
	var windowHeight = 0;
	if (typeof(window.innerHeight) == 'number')
		windowHeight = window.innerHeight;
	else
		if (document.documentElement && document.documentElement.clientHeight)
			windowHeight = document.documentElement.clientHeight;
		else
			if (document.body && document.body.clientHeight)
				windowHeight = document.body.clientHeight;
	
	return windowHeight;
}

function getWindowWidth() {
	var windowWidth = 0;
	if (typeof(window.innerWidth) == 'number')
		windowWidth = window.innerWidth;
	else
		if (document.documentElement && document.documentElement.clientWidth)
			windowWidth = document.documentElement.clientWidth;
		else
			if (document.body && document.body.clientWidth)
				windowWidth = document.body.clientWidth;
	
	return windowWidth;
}

function minWidth() {
	if (document.getElementById("main")) {
		if (getWindowWidth()<=970) {
			if (document.getElementById("main")) {
				document.getElementById("main").style.left=0+"px";
				document.getElementById("main").style.marginLeft=0+"px";
			}
		} else {
			if (document.getElementById("main")) {
				document.getElementById("main").style.left=50+"%";
				document.getElementById("main").style.marginLeft=-485+"px";
			}
		}
		
		if (getWindowHeight()<=590) {
			if (document.getElementById("main")) {
				document.getElementById("main").style.top=0+"px";
				document.getElementById("main").style.marginTop=0+"px";
			}
		} else {
			if (document.getElementById("main")) {
				document.getElementById("main").style.top=50+"%";
				document.getElementById("main").style.marginTop=-295+"px";
			}
		}
	}
}

function fixSample() {
    return;
	if (document.getElementById("sample") && document.getElementById("contentDetail")) {
		height = document.getElementById("contentDetail").offsetHeight;
		sampleHeight = document.getElementById("sample").getElementsByTagName("img")[0].height;
		
		if(height > sampleHeight) {
			document.getElementById("sample").style.height = height + "px";
			document.getElementById("sample").getElementsByTagName("img")[0].style.marginTop = Math.round((height-sampleHeight)/2) + "px";
		}
	}
}

var old;
function deploy(me) {
	current = me.parentNode.getElementsByTagName("ul")[0];
	if(current) {
		(current.style.display == "block") ? current.style.display="none" : current.style.display="block";
		me.className = "on";
		
		if(old && old != current) {
			old.style.display = "none";
			old.parentNode.getElementsByTagName("a")[0].className = "";
		}
		
		old = current;
		
		fixSample();
	}
}

var defaultSample;
function changeSample(img) {
	if(!defaultSample)
		if(document.getElementById("sampleImg"))
			defaultSample = document.getElementById("sampleImg").getAttribute("src");
	
	if(document.getElementById("sampleImg"))
		document.getElementById("sampleImg").setAttribute("src",img);
}

function fixDefaultSample() {
	if(document.getElementById("sampleImg"))
		document.getElementById("sampleImg").setAttribute("src",defaultSample);
}

function showHideTxt(me,action) {
	if(action == "show") {
		next = me.nextSibling;
		while(next != null) {
			if(next.nodeName == "DIV") {
				next.style.visibility = "visible";
				current = next;
				break;
			}
			next = next.nextSibling;
		}
	} else if(action == "hide") {
		current.style.visibility = "hidden";
	}
}