var ytplayer, activeEl;

function loadAndPlayYTI(url, el, id) {
	activeEl = el;
	$('#ytr').remove();
	$('.ytri').removeClass('focus');
	$('#' + el).html('<div id="' + el + '-movie"></div>');
    var params = { allowScriptAccess: "always", "wmode": "transparent" };
    var atts = { id: "ytr" };
    swfobject.embedSWF(url, el + '-movie', $('#' + el).width(), $('#' + el).height(), "8", null, null, params, atts);
}

function loadAndPlay(url) {
	$('#movie-container').html('<div id="movie"></div>');
    var params = { allowScriptAccess: "always", "wmode": "transparent" };
    var atts = { id: "myytplayer" };
    swfobject.embedSWF(url, "movie", "749", "446", "8", null, null, params, atts);
}

function onYouTubePlayerReady(playerid) {
	if (playerid == "ytplayer") {
		ytplayer = document.getElementById('myytplayer');
		$('#movie-container').addClass('focus');
	}
	if (playerid == "ytr") {
		ytplayer = document.getElementById('ytr');
		$('#'+activeEl).addClass('focus');
	}
	ytplayer.playVideo();
}

function toggleStep(area, current) {
	var current = $(current);
	var area = $("#"+area);
	
		
	$(".consumer button").removeClass("buttonhover");
	current.addClass("buttonhover");
	
	if(visible(area)) {
		
		$(".accordion:visible").slideUp("fast", function() {});
		area.slideDown();
	} else {
		area.slideUp("fast");
	}
}
function visible(e) {
	if($(e).css("display") != "none") {
		return false;	
	} else {
		return true;
	}
}

$(document).ready(function() {
	$("p:last-child").addClass("last");
	$("ul.tabs li:last-child").addClass("last");
	$("#thumbnails_list li:odd").addClass("last");
	$("td:last-child").addClass("text_right");
	$("th:last-child").addClass("text_right");
	$('#productmenu ul.menu li:first-child').addClass("firstmenuitem");
	$('#productmenu ul.menu li:last-child').addClass("lastmenuitem");

	var loopImgsButtons = '';
	$("div.loopImgs").children().each(function() {
		var oki = false;
		var tn = this.tagName.toLowerCase();
		if (tn == 'img') {
			oki = true;
		}
		if (tn == 'a') {
			$(this).children().each(function() {
				if (this.tagName.toLowerCase() == 'img') {
					oki = true;
				}
			});
		}
		if (oki == true) {
			$(this).wrap("<div class=\"image\"></div>");
			loopImgsNum++;
			//loopImgsButtons += '<div class="button">' + loopImgsNum + '</div>';
			loopImgsButtons += '<div class="button num_' + loopImgsNum + '" title="' + loopImgsNum + '"></div>';
		} else {
			$(this).remove();
		}
	});
	$("div.loopImgs").append('<div id="loopImgsButtons">' + loopImgsButtons + '</div>');

	$("div.loopImgs div.button").click(function () {
		$('#movie-container').removeClass('focus');
		if (ytplayer != null) {
			ytplayer.stopVideo();
		}
		loopImgsNext(parseInt($(this).attr('title'))-1);
	});
	
	$("div.loopImgs").append('<div id="movie-container"><div id="movie"/></div>');
	$("div.loopImgs div.image a[href^='http://youtube'], div.loopImgs div.image a[href^='http://www.youtube']").click(function() {
		//console.log($(this).attr('href'));
		loadAndPlay($(this).attr('href'));
		return false;
	});
	
	
	/*$("div.loopImgs").mouseover(function () {
		clearTimeout(loopImgsTimeout);
	});
	$("div.loopImgs").mouseout(function () {
		loopImgsTimeout = setTimeout("loopImgsNext(-1);", 0);
	});*/
	
	$('table#contact input, table#contact textarea').each(function() {
		if ($(this).attr('type') != "hidden") {
			if ($(this).val() == "") {
				$(this).val($(this).attr('title'));
			}
			$(this).bind("focus", function() {
				if ($(this).val() == $(this).attr('title')) {
					$(this).val("");
				}
			});
			$(this).bind("blur", function() {
				if ($(this).val() == "") {
					$(this).val($(this).attr('title'));
				}
			});
		}
	});
	
	$("input[@type=file]").each(function () {
		var link = $("<a/>").addClass("attachment").html('Bifoga bild (valfritt)').attr('name', $(this).attr('name')).click(function () {
			var file = $("<input type='file'/>").addClass('nicefile').attr({name: $(this).attr('name')});
			$(this).after(file);
			$(file).change(function () {
				$(this).addClass('hidden');
				$('#count').html('Antal bifogade bilder: ' + $('input[@type=file]').length);
			});
		});
		$(this).after(link);
		var count = $("<div/>").attr('id', 'count');
		$(link).after(count);
		$(this).remove();
	});
	
})

var loopImgsIndex = -1;
var loopImgsNum = 0;
var loopImgsTimeout;
function loopImgsNext (showId) {
	if (loopImgsIndex != null) {
		var fadeOut = loopImgsIndex;
		$("div.loopImgs div.image:eq(" + fadeOut + ")").fadeOut("slow", function () {
			$("div.loopImgs div.button:eq(" + fadeOut + ")").removeClass('selected');
		});
	}
	
	if (showId < 0) {
		loopImgsIndex++;
		if (loopImgsIndex==loopImgsNum) { loopImgsIndex = 0; }
	} else {
		loopImgsIndex = showId;
		clearTimeout(loopImgsTimeout);
	}

	$("div.loopImgs div.image:eq(" + loopImgsIndex + ")").fadeIn("slow", function () {
		$("div.loopImgs div.button:eq(" + loopImgsIndex + ")").addClass('selected');
		//loopImgsTimeout = setTimeout("loopImgsNext(-1);", 3000);
	});
}

$(window).bind('load', function () {
	
	$("a[href^='http://youtube'], a[href^='http://www.youtube']").each(function(index, item) {
		var img = $(this).find('img');
		if ($(img).length > 0) {
			var obj = $('<div></div>').addClass('ytri').attr('id', 'ytr-' + index).height($(img).height()).width($(img).width());
			$(this).before(obj);
			$(this).click(function () {
				loadAndPlayYTI($(this).attr('href'), 'ytr-' + index, 'yti-player-' + index);
				return false;
			});
		}
	});
	
	/*$('embed').each(function(index, item) {
		$(this).after($('<div/>').attr('id', 'embed-' + index));
		var params = { allowScriptAccess: "always", play: "true", loop: "true", menu: "true" };
	    var atts = {};
	    swfobject.embedSWF($(this).attr('src'), 'embed-' + index, $(this).attr('width'), $(this).attr('height'), "8", null, null, params, atts);
	    $(this).remove();
	});*/


	$("div.loopImgs div.image").fadeOut();
	
	var maxHeight = 0;
	$("div.loopImgs div.image").each(function () {
		var divHeight = $(this).find('img').height();
		if ($(this).find('img.hover').length > 0) {
			divHeight = (divHeight) / 2;
			$(this).height(divHeight);
			$(this).find('img').bind('mouseenter', function() {
				//alert($(this).parents('div.image').height());
				//alert($(this).parents('div.image').find('img').height());
				$(this).css('margin-top', '-' + $(this).parents('div.image').height() + 'px');
			});
			$(this).find('img').bind('mouseleave', function() {
				$(this).css('margin-top', '0');
			});
		} else {
			$(this).height(divHeight);
		}
		if (divHeight > maxHeight) { maxHeight = divHeight; }
	});
	maxHeight += 3+3;
	$("#loopImgsButtons").css('margin-top', maxHeight + 'px');
	maxHeight += 25;
	$("div.loopImgs").height(maxHeight);
	
	//$("div.loopImgs div.image").css('display', 'block');
	/*$("div.loopImgs").height($("div.loopImgs div.image:eq(0)").height());*/
	/*$("div.loopImgs").width($("div.loopImgs div.image:eq(0)").width());*/
	loopImgsNext(-1);
});

