var timer;
var delay = 1000;
var visibleCutout = null;
var circleWidth = 26;
var circleLimit = 12;

$(document).ready(function() {
	initCutouts();
	initNavigation();

	if (hasContent < 1) {
		$("div#inner-content").css('backgroundImage', 'none');
		$("div#inner-content div.content-body").css('backgroundImage', 'none');
	}

	$("div#inner-content").css('visibility', 'visible');

	if (page != 'home')
		refreshScrollbar();

	$("label.overlabel").overlabel();
		initSubmits();

	if (hasCircles > circleLimit)
		initArrows();

	if (page == 'lookbook') {
		$("div#marquee-wrapper").css('z-index', 2);
		$("div#inner-wrapper").css('z-index', 1);
		
		$('#inner-content div.misc div#circles div#circle-0 a').click();
	}
});

function initCutouts() {
	$("div#cutouts div.cutout").each(function(index) {
		$(this).css('display', 'none');
		$(this).css('visibility', 'visible');
	});
}

function initNavigation() {
	$("#nav ul li a").each(function(index) {
		if (!$(this).hasClass('selected')) {
			$(this).hover(function() {
				showCutout(index);
			}, function() {
				autoHide();
			});
		}

		if ($(this).hasClass('ignore') || $(this).hasClass('selected')) {
			$(this).removeAttr('href');
		}
	});
}

function refreshScrollbar() {
	if ($('#mcs_container').length > 0)
		$("#mcs_container").mCustomScrollbar("vertical",300,"easeOutCirc",1.2,"fixed","yes","no",15);
}

function refreshCircles(id) {
	$("div#circles div.circle").each(function(index) {
		var a = $(this).find('a');
		var i = a.attr('id').split(/\-/g);

		if (i[2] == id)
			a.addClass('selected');
		else
			a.removeClass('selected');
	});
}

function showCutout(id) {
	if (visibleCutout != null) {
		visibleCutout.unbind('mouseenter mouseleave');
		clearTimeout(timer);
	}

	$("#cutouts div.cutout").each(function(index) {
		if (index == id) {
			$(this).fadeIn('fast', fadeComplete);
			visibleCutout = $(this);

			if (id == 6) {
				$("div#cutouts div#contact input, div#cutouts div#contact textarea").bind('mouseenter', function() {
					clearTimeout(timer);
				});
			}
		} else {
			$(this).css('display', 'none');
		}
	});
}

function hideCutout(id) {
	if (visibleCutout != null) {
		visibleCutout.fadeOut(100, function() {
			clearTimeout(timer);

			if (id > -1) {
				showCutout(id);
			}
		});
	}
}

function fadeComplete() {
	$(this).bind('mouseenter', function() {
		clearTimeout(timer);
	}).bind('mouseleave', function() {
		autoHide();
	});
}

function autoHide() {
	timer = setTimeout(function() {
		hideCutout(-1);
	}, delay);
}

function initSubmits() {
	$("div#cutouts div#contact a#submit-contact").click(function() {
		var form = $("form#contact-form").serialize() + "&type=contact";
		$.post("submission.php", form, function(data) {
			$("form#contact-form div:last").html(data);
		});
	});

	$("div#cutouts div#contact a#submit-subscribe").click(function() {
		var form = $("form#subscribe-form").serialize() + "&type=subscribe";
		$.post("submission.php", form, function(data) {
			$("form#subscribe-form div:last").html(data);
		});
	});
}

var current = 0;
var last;
var tweening = false;

function initArrows() {
	last = Math.floor(hasCircles / circleLimit);
	$("#inner-content div.misc div#circles").css('width', circleWidth * (circleLimit / 4).toString() + 'px');

	$("div#circles-right a").click(function() {
		if (!tweening)
			scrollCircles(1);
	});

	$("div#circles-left a").click(function() {
		if (!tweening)
			scrollCircles(-1)
	});
}

function scrollCircles(value) {
	tweening = true;

	var target;
	var sides = [];

	sides.push('-' + $("#inner-content div.misc div#circles").css('width'));
	sides.push($("#inner-content div.misc div#circles").css('width'));

	if (value > 0) {
		if (current < last) {
			$("div#circle-container").animate({ left: sides[0] }, 'slow', function() {
				target = -(circleWidth * ++current) + "px";
				$(this).css('left', sides[1]).css('top', target);
				$(this).animate({ left: 0 }, 'fast', function() {
					tweening = false;
				});
			});
		} else {
			tweening = false;
		}
	} else {
		if (current > 0) {
			$("div#circle-container").animate({ left: sides[1] }, 'slow', function() {
				target = -(circleWidth * --current) + "px";
				$(this).css('left', sides[0]).css('top', target);
				$(this).animate({ left: 0 }, 'fast', function () {
					tweening = false;
				});
			});
		} else {
			tweening = false;
		}
	}
}

var mobileWindow;
readInWindow = function(page, subpage) {
	mobileWindow = window.open('index.php?page=' + page + '&sub=' + subpage + '&mobile=1', 'mobileWindow', 'toolbar=0,location=0,status=1,menubar=0,scrollbars=1,resizable=0,width=606,height=300');
}

$.fx.prototype.cur = function(){
    if ( this.elem[this.prop] != null && (!this.elem.style || this.elem.style[this.prop] == null) ) {
      return this.elem[ this.prop ];
    }
    var r = parseFloat( jQuery.css( this.elem, this.prop ) );
    return typeof r == 'undefined' ? 0 : r;
}
