startUrl=(window.location).toString();
splitUrl=startUrl.split("/");
myUrl="";
for(i=0;i<(splitUrl.length)-1 ;i++) {
	myUrl=myUrl+splitUrl[i];
	if (i!=(splitUrl.length)-2){
		myUrl=myUrl+"/";
	}
}
abspath=myUrl;

$(document).ready(function() {
	//hover top banners
	$('#top ul:first>li').hover(function() {
			$(this)
				.addClass('active')
				.prepend('<p></p>')
				.find('p')
				.fadeIn('fast');
		}, function() {
			$(this)
				.removeClass('active')
				.find('p')
				.fadeOut('fast', function() {
					$(this).remove()
				});
	});
	
	//hover iPhone banner
	$('#iPhonebanner').hover(function() {
			$('span', $(this)).fadeIn('fast');
		}, function() {
			$('span', $(this)).fadeOut('fast');
	});
	
	//hover social network links
	$('#sn a').hover(function() {
		$(this)
			.append('<p></p>')
			.find('p')
			.fadeIn('fast');
		}, function() {
		$(this)
			.find('p')
			.fadeOut('fast', function() {
				$(this).remove()
		});
	});
	
	topWidth = $('#banTop li').length * tbw;
	btmWidth = $('#banBot li').length * bbw;
	
	$('#menu ul li ul').hide();

	$('#topContainer').width(topWidth);
	$('#btmContainer').width(btmWidth);
	
	longDiv = (topWidth > btmWidth) ? $('#topContainer') : (btmWidth > topWidth) ? $('#btmContainer') : null;
	shortDiv = (longDiv) ? $('#banners>div').not('#'+longDiv.attr('id')) : null;
	
	if(Math.max(topWidth, btmWidth) > $('#banners').width()) {
		if(Math.min(topWidth, btmWidth) > $('#banners').width() ) {
		moveBlock(shortDiv).floatRight();
		}
		else {
		moveBlock(shortDiv).floatLeft();
		}
	}
	
	$('#banners').jScrollPane();
		
	$('ul#banTop li a').hover(function() {
		var el = $(this);
		var banId = el.find('img.title').attr('src');
		var path = banId.substring(0,banId.lastIndexOf('/')+1);
		banId = banId.substring(banId.lastIndexOf('/')+1);
		banId = banId.substring(0,banId.lastIndexOf('_'));

		var text = el.find('img.title').attr('alt');
		el.find('img.on').fadeIn('fast').end().find('img.title').attr({src: path+banId+'_on.gif'});
		el.append(overDiv).find('.over').css({opacity:0.7}).fadeIn('fast').end().find('p.desc').text(text);
		}, function() {
		var el = $(this);
		var banId = el.find('img.title').attr('src');
		var path = banId.substring(0,banId.lastIndexOf('/')+1);
		banId = banId.substring(banId.lastIndexOf('/')+1);
		banId = banId.substring(0,banId.lastIndexOf('_'));
		el.find('img.on').fadeOut('fast');
		el.find('img.title').attr({src: path+banId+'_off.gif'});
		$('.over, p.desc').fadeOut('fast', function() {
			$(this).remove();
			});
		});
	
	$('ul#banBot li a').hover(function() {
		$(this).find('img.on').fadeIn('fast'); 
		}, function() {
		$(this).find('img.on').fadeOut('fast');
		});
		
	$('a#language').hover(function() {	
		$(this).addClass('active');
		$('ul.stores').hide();
		$('a#st_locator').removeClass('active');
		$('ul.language').fadeIn('slow');
		inMenu = true;
	}, function() {
		inMenu = false;
		window.setTimeout("closeLang()",1000);
	});
	
	$('a#st_locator').hover(function() {	
		$(this).addClass('active');
		$('ul.language').hide();
		$('a#language').removeClass('active');
		$('ul.stores').fadeIn('slow');
		inMenu = true;
	}, function() {
		inMenu = false;
		window.setTimeout("closeStores()",1000);
	});
	
	$('ul.language').hover(function() {	
		inMenu = true;
	}, function() {
		inMenu = false;
		window.setTimeout("closeLang()",500);
	});
	
	$('ul.stores').hover(function() {	
		inMenu = true;
	}, function() {
		inMenu = false;
		window.setTimeout("closeStores()",500);
	});
	
	$(window).resize(function() {
		clearTimeout(f);
		setLayout();
		if(Math.max(topWidth, btmWidth) > $('#banners').width()) {
			if(Math.min(topWidth, btmWidth) > $('#banners').width() ) {
				moveBlock(shortDiv).floatRight();
				}
			else {
			moveBlock(shortDiv).floatLeft();
			}
		}
	});
	
	listenClick();
	
});

function closeLang() {
	if(inMenu == false) {
		$('ul.language').fadeOut('slow', function() {
			$('a#language').removeClass('active')
			});
		}
	}
	
function closeStores() {
	if(inMenu == false) {
		$('ul.stores').fadeOut('slow', function() {
			$('a#st_locator').removeClass('active')
			});
		}
	}
	
function setLayout() {	
	$('#banners').width($('#wrapper').innerWidth() - 285).jScrollPane();
	//setHeight();
	}
	
function setHeight() {	
	var wh = $(window).height();
	if(wh > 650 ) {
		$('#top, #bottom').height(wh/2 - 5);
		}
	else {
		$('#top').height('300px');
		}
	}
	
	
function listenClick() {
	/*
	$('#banners a').not($('.plainwin')).click(function() {
		var url = $(this).attr('href');
		openNew(url);
		return false;
		});
		
	$('#menu ul a').not($('ul.language a,a#language,a#swide,a#store')).click(function() {
		var url = $(this).attr('href');
		if($(this).attr('id') == 'DGJunior') {
			openNoResize(url);
			}
		else {
			openNew(url);
			}
		return false;
		});
	*/	
		
	$('#footer a').not($('#sitemap,#onlinestore')).click(function(e) {
		var url = $(this).attr('href');
		e.preventDefault;
		window.open(url,'new','resizable=no,scrollbars=yes,status=no,width=500,height=420');
		return false;
		});
		
	}

function openNew(url) {
	window.open(url,'neww','left=0,top=0,menubar=no,location=yes,titlebar=no,directories=no,resizable=yes,scrollbars=no,status=no, width='+screen.width+', height='+screen.height);
	}

function openNoResize(url) {
	window.open(url,'neww','left=0,top=0,menubar=no,location=yes,titlebar=no,directories=no,resizable=no,scrollbars=no,status=no, width='+screen.width+', height='+screen.height);
	}
	
function openFashionShow(url) {
	window.open(url,'fashionshoww','left=0,top=0,menubar=no,location=yes,titlebar=no,directories=no,resizable=yes,scrollbars=no,status=no, width='+screen.width+', height='+screen.height);
}
	
function moveBlock(div)
{
	var el = div[0];
	var ww = $('#banners').outerWidth();
	var ew = div.width();
	window[div.attr('id') + "_obj"] = el;
	el.cx = parseInt(div.css('left'));
	el.move = function(x){
		if(x>0) div.css({left: x});
		}

	el.floatRight=function()
	{
		var pX = $('#banners').css('left').slice(0,-2);
		var dist = ew - (-pX + ww);
		dist = (dist>0) ? 0 : dist;
		if (dist<=0) this.cx += (-dist - this.cx)/10;
		this.move(this.cx);
		f = setTimeout(this.id + "_obj.floatRight()", 30);
	}
	
	el.floatLeft=function()
	{
		var pX = $('#banners').css('left').slice(0,-2);
		this.cx += (-pX - this.cx)/10;
		this.move(this.cx);
		f = setTimeout(this.id + "_obj.floatLeft()", 30);
	}
	return el;
}

var overDiv = '<div class="over"></div><p class="desc"></p>';
var inMenu = false;
var topW = 0;
var botW = 0;
var tbw = 222; // top banner item width;
var bbw = 174; // bottom banner item width;