/*************************************
	Edison:Code Custom Site Script
	Client: Magenta Restaurant
	www.edisoncode.com
	Copyright 2011
*************************************/

function positionNav(element) {
	var angle = 153;
	var angleInc = 5.8;
	var radius = 405;
	var oldx = 566;
	var oldy = 302;
	var width;
	var height;
	var radians;
	
	element.children().each( function(index) {
		var me = $(this);
		width = me.width();
		height = me.height();
		radians = (angle/180) * Math.PI;
		var x = Math.floor(Math.cos(radians) * radius);
		var y = Math.floor(Math.sin(radians) * radius);
		
		var newx = (oldx + x) - width;
		var newy = (oldy - y) - height;
		
		/*if( me.hasClass('active') && me.html().indexOf('SNUGbar') >= 0) {
			angle = angle + 2;
		} else {*/
			if( me.hasClass('active')) {  //&& me.html().indexOf('Contact') >= 0
			angle = angle + 2;
			} else {
			angle = angle + angleInc;}
		//}

		me.css('top', newy + 'px');
		me.css('left', newx +'px');
	});
}

function positionContent(element) {
	var angle = 162;
	var angleInc = 3.4;
	var mod = 0.30;
	var radius = 370;
	var oldx = 383;
	var oldy = 383;
	var lasty = 0;
	var width;
	var height;
	var radians;
	
	element.children('.line').each( function(index) {
		var me = $(this);
		width = me.width();
		height = me.height();
		radians = (angle/180) * Math.PI;
		var x = Math.floor(Math.cos(radians) * radius);
		var y = Math.floor(Math.sin(radians) * radius);
		
		var newx = (oldx + x) + (height/2);
		var newy = (oldy - y) - (height/2);
		
		if( lasty != 0 ) {
			while (newy - lasty > 20) {
				angle = angle - .02;
				radians = (angle/180) * Math.PI;
				var x = Math.floor(Math.cos(radians) * radius);
				var y = Math.floor(Math.sin(radians) * radius);
				
				var newx = (oldx + x) + (height/2);
				var newy = (oldy - y) - (height/2);
			}
			
			while (newy - lasty < 20) {
				angle = angle + .02;
				radians = (angle/180) * Math.PI;
				var x = Math.floor(Math.cos(radians) * radius);
				var y = Math.floor(Math.sin(radians) * radius);
				
				var newx = (oldx + x) + (height/2);
				var newy = (oldy - y) - (height/2);
			}
		}
		
		if ( width == 0) {
			angle = angle + 2.5;
			lasty = 0;
		}else {
			angle = angle + angleInc;
			me.attr('data-gap', newy - lasty);
			lasty = newy;
		}
		
		
		
		if( angle > 200) { 
			angle = angle + mod;
			mod = mod + .06;
		};
		
		me.css('top', newy + 'px');
		me.css('left', newx +'px');
	});
}

function scaleWrapper() {
	var wrapper = $('.wrapper');
	var address = $('.address');
	var footer = $('.footer');
	var wedMap = $('img.wedMap');
	var maxHeight = $(window).height();
		
	if(maxHeight >= 684) {
		wrapper.css('height', '684px');
		footer.css('bottom', '45px');
		address.css('bottom', '45px');
		var left = (( $('.content').width() / 2 ) + 183 ) - (footer.width() / 2);
		var bottom = wedMap.height() + 2;
		footer.css('left', left + 'px');
		left = left - address.width() - 110;
		address.css('left', left + 'px');
		$('.city').css('margin-left', '35px');
		$('.phone').css('margin-left', '125px');
		$('.facebook').css('left', '528px');
		$('.facebook').css('bottom', '12px');
	}
	
}

function rand(from, to, current) {
	var random = Math.floor(Math.random() * (to - from + 1) + from);
	if (random == current) {random = random + 1; }
	if (random > to) { random = random - 2; }
	return Math.floor(Math.random() * (to - from + 1) + from); 
}

function validImages(top, middle, bottom, cur1, cur2, cur3) {
	var max = 17;
	if ( top != middle && top != bottom && middle != bottom && (top!= cur1 && middle != cur2 && bottom != cur3)) {
		if (top <= max && top > 0 && middle <= max && middle > 0 && bottom <= max && bottom > 0) {
			return true;
		} else { return false; }
	} else { return false; }
}

function imageSwap() {
	var maxNum = 17; 
	
	var $top = $('.imageTop');
	var $topFade = $top.children('img');
	var $mid = $('.imageMiddle');
	var $midFade = $mid.children('img');
	var $btm = $('.imageBottom');
	var $btmFade = $btm.children('img');
		
	var i = $btm.children('img').attr('src').lastIndexOf('/') + 1;
	var path = $btm.children('img').attr('src').substring( 0 , i ); 
	var btmCur = parseInt($btm.children('img').attr('src').substring(i + 5, i + 6));
	var midCur = parseInt($mid.children('img').attr('src').substring(i + 5, i + 6));
	var topCur;
	if ($('.navigation ul').children('.active').html().indexOf('Contact') == -1) {
		topCur = parseInt($top.children('img').attr('src').substring(i + 5, i + 6));
	} else { topCur = 1; }
		
	var rand1 = rand(0, maxNum, topCur);
	var rand2 = rand(0, maxNum, midCur);
	var rand3 = rand(0, maxNum, btmCur);
	
	var count = 0;
	
	while (validImages(rand1, rand2, rand3, topCur, midCur, btmCur) != true) {
		
		if (count > 50) {						
			rand1 = (topCur + 1) > maxNum ? topCur - 1: topCur + 1;
			rand2 = (midCur + 1) > maxNum ? midCur - 1: midCur + 1;
			rand3 = (btmCur + 1) > maxNum ? btmCur - 1: btmCur + 1;
		}else {
			rand1 = rand(0, maxNum, topCur);
			rand2 = rand(0, maxNum, midCur);
			rand3 = rand(0, maxNum, btmCur);
		}
		count += 1;
	}
		
	var $nextTop = $('<img />').attr('src', path + 'image' + rand1 + '_295.png');
	var $nextMid = $('<img />').attr('src', path + 'image' + rand2 + '_236.png');
	var $nextBtm = $('<img />').attr('src', path + 'image' + rand3 + '_204.png');
	
	$nextTop.css('z-index', '5');
	$nextMid.css('z-index', '5');
	$nextBtm.css('z-index', '5');
	$top.append($nextTop);
	$mid.append($nextMid);
	$btm.append($nextBtm);
	
	$btmFade.fadeOut("slow", function() { 
		$btmFade.remove();
		$btm.children('img').css('z-index', '10');
		$midFade.fadeOut("slow", function() { 
			$midFade.remove();
			$mid.children('img').css('z-index', '10');
			$topFade.fadeOut("slow", function() { 
				$topFade.remove();
				$top.children('img').css('z-index', '10');
			} );
		} );	
	} );
}

function ieFixes() {
	var supports = (function() {  
	var div = document.createElement('div'),  
		vendors = 'Khtml Ms O Moz Webkit'.split(' '),  
		len = vendors.length;  
		return function(prop) {  
		  if ( prop in div.style ) return true;  
	  
		  prop = prop.replace(/^[a-z]/, function(val) {  
			 return val.toUpperCase();  
		  });  
	  
		  while(len--) {  
			 if ( vendors[len] + prop in div.style ) {  
				// browser supports box-shadow. Do what you need.  
				// Or use a bang (!) to test if the browser doesn't.  
				return true;  
			 }  
		  }  
		  return false;  
		};  
	})();  
    
	if ( supports('border-radius') ) { 
	   //Do nothing, we're good! 
	} else {
		$('.content').css('background', 'transparent url(assets/images/ie8_bg.png) no-repeat');
		$('.contact').css('background', 'transparent url(assets/images/contact_bg.png) 0px -4px no-repeat');
		$('.private').css('display', 'block');
	}
}

$(document).ready( function() {
	var navList = $('.navigation ul');
	var content = $('.content');
	positionNav(navList);
	positionContent(content);
	scaleWrapper();
	
	ieFixes();
		
	$('[data-type=modal]').click( function() {
		var me = $(this);
		var modal = $('.modalDialog');
		var shadow = $('.modalShadow');
		var menu = me.attr('data-menu') + '.htm';
		shadow.removeClass('hidden');
		modal.load(menu, function() { 
			$('.modalWrap').removeClass('hidden');
		});
		shadow.click(function() { $('.modalWrap').addClass('hidden'); shadow.addClass('hidden'); });
		$('.modalClose').click(function() { $('.modalWrap').addClass('hidden'); shadow.addClass('hidden'); });
	});
	
	$('[data-type=map]').click( function() {
		var modal = $('.modalDialog');
		var shadow = $('.modalShadow');
		shadow.removeClass('hidden');
		modal.load('map.htm', function() { 
			$('.modalWrap').removeClass('hidden');
		});
		shadow.click(function() { $('.modalWrap').addClass('hidden'); shadow.addClass('hidden'); });
		$('.modalClose').click(function() { $('.modalWrap').addClass('hidden'); shadow.addClass('hidden'); });
	});
	
	$('[data-type=contact]').click( function() {
		var modal = $('.modalDialog');
		var shadow = $('.modalShadow');
		shadow.removeClass('hidden');
		modal.load('contact.php', function() { 
			$('.modalWrap').removeClass('hidden');
		});
		shadow.click(function() { $('.modalWrap').addClass('hidden'); shadow.addClass('hidden'); });
		$('.modalClose').click(function() { $('.modalWrap').addClass('hidden'); shadow.addClass('hidden'); });
	});
	
	setInterval('imageSwap()', 8000);
	
	navList.children('.private').fadeIn('slow');
	
	/*
	var $active = navList.children('.active')
	
	if ( $active.html() == 'SNUGbar') {
		navList.children('.private').fadeIn('slow');
	}
	if ( $active.html() == 'Contact') {
		navList.children('.private').fadeIn('slow');
	}*/
	
});
