var $j = jQuery.noConflict(); 

var loopImgsIndex = -1;
var loopImgsNum = 0;
var loopCounter = 0;

function loggedIn() {
	$j(document).ready(function() {
	    var obj = $j('.navadmin li a[href=/customer/account/login]');
	    $j(obj).text('Logga ut');
	    $j(obj).attr('href', '/customer/account/logout');
	});
}

$j(document).ready(function() {
	//$j('.dashboard .box').removeClass('box');
	
	Validation.add('validate-orgnr', 'Please enter a valid organisation number. For example 554433-1122.', function (v) {
		return Validation.get('IsEmpty').test(v) || /^\d{6}-?\d{4}$/.test(v);
	});

        //MOD: these lines where uncommented by Improove when manufacturer select was refactored
	//$j('#brandselectManufactures option:eq(0)').text($j('#brandselectManufactures').prev('h1').text());

	/*$j("div.loopImgs").children().each(function() {
		var oki = false;
		var tn = this.tagName.toLowerCase();
		if (tn == 'img') {
			oki = true;
		}
		if (tn == 'a') {
			$j(this).children().each(function() {
				if (this.tagName.toLowerCase() == 'img') {
					oki = true;
				}
			});
		}
		if (oki == true) {
			$j(this).wrap("<div class=\"image\"></div>");
			loopImgsNum++;
		} else {
			$j(this).remove();
		}
	});*/

	$j("div.loopImgs div.image").each(function() {
		loopImgsNum++;
	});
	
	if ($j("#brandbox").length > 0) {
		var move = null;
		$j('body').attr('class').split(' ').each(function(item) {
		    var parts = item.split('-');
		    if (parts[0] == 'categorypath' && parts.length < 4 && move == null) {
		        move = true;
		    }
		});
	
		if (move == true) {
		    //$j('.col-left').prepend($j('.col-main > .box'));
			$j('.col-main').prepend($j('.col-left .box:first-child'));
		}
		
		brandsStart = $j("#brandbox").height();
		$j("#brandbox_more").css('opacity','0');
		$j("#brandbox_more").css('display','block');

                //MOD: these lines where uncommented by Improove when manufacturer select was refactored
		//document.getElementById('brandselectManufactures').options[currentCategoryIndex].selected=true;
		//viewProducts (currentCategory);
	}
});

var brandsStart;
function moreOrLessBrands () {
	var statement = $j("#brandbox").height()==brandsStart;

	$j("#brandbox_more").animate({
		opacity: (statement?1:0)
	});

	$j("#brandbox").animate({
		height: ((statement?($j("#brandbox").height() + $j("#brandbox_more").height()):brandsStart)) + 'px'
	},500,'',function () {
		document.getElementById('moreOrLessBrandsA').innerHTML = (statement?$j('#translateLessBrands').text():$j('#translateMoreBrands').text());
	});
}


function loopImgsNext () {
	if (loopImgsIndex != null) {
		$j("div.loopImgs div.image:eq(" + loopImgsIndex + ")").fadeOut("slow");
	}
	loopImgsIndex++;
	if (loopImgsIndex==loopImgsNum) {
		loopCounter++;
		loopImgsIndex = 0;
	}

	$j("div.loopImgs div.image:eq(" + loopImgsIndex + ")").fadeIn("slow", function () {
		//if (loopCounter <= 2) {
			setTimeout("loopImgsNext();", 3000);
		//};
	});
}

$j(window).bind('load', function () {
	$j("div.loopImgs div.image").fadeOut();
	$j("div.loopImgs div.image").css('display', 'block');
	$j("div.loopImgs").height($j("div.loopImgs div.image:eq(0)").height());
	$j("div.loopImgs").width($j("div.loopImgs div.image:eq(0)").width());
	loopImgsNext();
});

function viewProducts (category) {
	var ProductObj = document.getElementById('brandselectProducts');
	ProductObj.options.length = 0;
	if (category != '') {
		ProductObj.options[0] = new Option($j('#brandselectProducts').prev('h1').text(), '', false, false);
		var selected = 0;
		for (i = 0; i < products[category].length; i++) {
			if(products[category][i][2] == currentProduct){
				selected = i+1;
			}
			ProductObj.options[i+1] = new Option(products[category][i][1], products[category][i][0], false, false);
		}
		ProductObj.options[selected].selected=true;

	}
}

function viewProduct (url) {
	window.location = url;
}
