/* 	
	This is the main JS that controls vital bits and pieces. 
	The code herein requires jQuery and jQuery.corners to oprerate correctly.
	
	Enjoy :)
*/

/* This is the stuff that happens when the page is finished messing around with the DOM */
$(document).ready( function(){

	/* Nav */
	$('ul.sf-menu').supersubs({
        minWidth:    10,   // minimum width of sub-menus in em units
        maxWidth:    35,   // maximum width of sub-menus in em units
        extraWidth:  2     // extra width can ensure lines don't sometimes turn over
                           // due to slight rounding differences and font-family
    }).superfish({
    	onShow:  function(){
    		// Figure out if we have room for this guy
    		var pos = $(this).offset();
    		var fromLeft = pos.left + $(this).width()
    		// If there's no room then move
    		if ( fromLeft > $(window).width() ) {
    			$(this).css('left', '-' + $(this).parent().width() + 'px');
    		}
    	}
    }).find('ul').bgIframe();

	/* Round them corners */
	$('.rounded').corners("5px");
	$('.roundedLeft').corners("left 5px");
	$('.rounded_bottom').corners("bottom 4px");
	
	/* Fix that IE6 */
	try {
		$(document).pngFix();
	} catch (e) { /* We only load this function for IE6 so don't worry about the error */ }

	/* Add our gallery functions */
	$('.dress > a').click( function(event) {

		slideSwitch(($(this).find('img').attr('src')).replace('_thumb',''), $(this).attr('href'));
		event.preventDefault();

	});
	$('#dress_showcase_view_front').click( function(event) {

		slideSwitch($('#dress_showcase .image_wrap a.active img').attr('src').replace('_back','_front'), $(this).attr('href'));
		$('#dress_showcase_view_back').removeClass('current');
		$('#dress_showcase_view_front').addClass('current');
		event.preventDefault();

	});
	$('#dress_showcase_view_back').click( function(event) {

		slideSwitch($('#dress_showcase .image_wrap a.active img').attr('src').replace('_front','_back'), $(this).attr('href'));
		$('#dress_showcase_view_front').removeClass('current');
		$('#dress_showcase_view_back').addClass('current');
		event.preventDefault();

	});

	if ( $("div.dress_showcase_random").length > 0 ) {

		setInterval("slideSwitch('', '')", 5000);

	}

	// select #flowplanes and make it scrollable. use circular and navigator plugins
	$("#flowpanes").scrollable({size: 1, clickable: false}).circular().navigator({

        // select #flowtabs to be used as navigator
        navi: "#flowtabs",

        // select A tags inside the navigator to work as items (not direct children)
        naviItem: 'a',

        // assign "current" class name for the active A tag inside navigator
        activeClass: 'current'

    }); 

});

function slideSwitch(target, href) {
    var active = $('#dress_showcase .image_wrap a.active');

	if ( active.length == 0 ) active = $('#dress_showcase .image_wrap a:last');

	if ( (target.length > 0) && ( href.length > 0) ) {

		$('#dress_showcase .image_wrap a:not(.active)').remove();
		var next = $("<a href=\"" + href + "\"><img src=\"" + target + "\"></a>");
		$('#dress_showcase .image_wrap').append(next);
		
	}

	next =  active.next().length ? active.next() : $('#dress_showcase .image_wrap a:first');

	$('#dress_showcase_view_back').attr('href', href);
	$('#dress_showcase_view_front').attr('href', href);
	$('#dress_showcase_view_back').removeClass('current');
	$('#dress_showcase_view_front').addClass('current');

	active.addClass('last-active');

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

