//Debug: 0

jQuery.fn.imageZoom=function(conf){var config=jQuery.extend({speed:200,dontFadeIn:1,hideClicked:1,imageMargin:30,className:'jquery-image-zoom',loading:'Loading...'},conf);config.doubleSpeed=config.speed/4; return this.click(function(e){var clickedElement=jQuery(e.target);var clickedLink=clickedElement.is('a')?clickedElement:clickedElement.parents('a');clickedLink=(clickedLink&&clickedLink.is('a')&&clickedLink.attr('href').search(/(.*)\.(jpg|jpeg|gif|png|bmp|tif|tiff)$/gi)!=-1)?clickedLink:false;var clickedImg=(clickedLink&&clickedLink.find('img').length)?clickedLink.find('img'):false;if(clickedLink){clickedLink.oldText=clickedLink.text();clickedLink.setLoadingImg=function(){if(clickedImg){clickedImg.css({opacity:'0.5'})}else{clickedLink.text(config.loading)}};clickedLink.setNotLoadingImg=function(){if(clickedImg){clickedImg.css({opacity:'1'})}else{clickedLink.text(clickedLink.oldText)}};var displayImgSrc=clickedLink.attr('href');if(jQuery('div.'+config.className+' img[src="'+displayImgSrc+'"]').length){return false}var preloadOnload=function(){clickedLink.setNotLoadingImg();var dimElement=clickedImg?clickedImg:clickedLink;var hideClicked=clickedImg?config.hideClicked:0;var offset=dimElement.offset();var imgzoomBefore={width:dimElement.outerWidth(),height:dimElement.outerHeight(),left:offset.left,top:offset.top};var imgzoom=jQuery('<div><img src="'+displayImgSrc+'" alt="" /></div>').css('position','absolute').appendTo(document.body);var imgzoomAfter={width:imgzoom.outerWidth(),height:imgzoom.outerHeight()};var windowDim={width:jQuery(window).width(),height:jQuery(window).height()};if(imgzoomAfter.width>(windowDim.width-config.imageMargin*2)){var nWidth=windowDim.width-config.imageMargin*2;imgzoomAfter.height=(nWidth/imgzoomAfter.width)*imgzoomAfter.height;imgzoomAfter.width=nWidth}if(imgzoomAfter.height>(windowDim.height-config.imageMargin*2)){var nHeight=windowDim.height-config.imageMargin*2;imgzoomAfter.width=(nHeight/imgzoomAfter.height)*imgzoomAfter.width;imgzoomAfter.height=nHeight}imgzoomAfter.left=(windowDim.width-imgzoomAfter.width)/2+jQuery(window).scrollLeft();imgzoomAfter.top=(windowDim.height-imgzoomAfter.height)/2+jQuery(window).scrollTop();var closeButton=jQuery('<a href="#">Close</a>').appendTo(imgzoom).hide();if(hideClicked){clickedLink.css('visibility','hidden')}imgzoom.addClass(config.className).css(imgzoomBefore).animate(imgzoomAfter,config.speed,function(){closeButton.fadeIn(config.doubleSpeed)});var hideImgzoom=function(){closeButton.fadeOut(config.doubleSpeed,function(){imgzoom.animate(imgzoomBefore,config.speed,function(){clickedLink.css('visibility','visible');imgzoom.remove()})});return false};imgzoom.click(hideImgzoom);closeButton.click(hideImgzoom)};var preload=new Image();preload.src=displayImgSrc;if(preload.complete){preloadOnload()}else{clickedLink.setLoadingImg();preload.onload=preloadOnload}return false}})};

$.fn.duplicate = function(n) {
	if( !n ) n = 1;
	for(var i = 0; i <= n; i++) {
		this.clone().insertAfter( this );
	}
}

$(function() {
	setInterval(function(a) {
		if(!$('#bridge').data('second')) {
			$('#bridge').data('second', true);
			$('#bridge').css('backgroundPosition', '0 0');
		} else {
			$('#bridge').data('second', false);
			$('#bridge').css('backgroundPosition', '0 400px');
		}
	}, 200);
	animateWave();
	var stickWraps = $('.stick_wrap');
	animateSticks( stickWraps[0] );
	animateSticks( stickWraps[3] );
	setTimeout(function() {
		animateSticks( stickWraps[1] );
		animateSticks( stickWraps[2] );
	}, 2000);
	
	robbie = RobbieRobo.init('images/characters/robbie_robo.png', function() {
		// this.css('display', 'none');
		this.place(650, 0);
		this.animation("slide", [
			{x: 150 },
			{x: 100, r:45},
			{x: 0, l: 1000},
			{x: -80, r: -45},
			{x: -170}
		]).animation("peek", [
			{x: 200, y: -120, r: 10, l: 400},
			{l: 700},
			{r: -30, x: -270, y: -30},
			{x: 0, l: 600},
			{y: 150, r: 20, x: 70}
		]);
	}, '#newsstand_detail');
	
	squaw = Squaw.init('images/characters/squaw.png', function() {
		this.place(500, 80);
		
		this.animation("leftquick", [
			{x: -50, r: -10, l: 20},
			{l: 700},
			{x: 50, r: 10, l: 20}
		]).animation("leftslow", [
			{x: -70, r: -2, l: 200},
			{l: 500},
			{x: -70, r: -6, l: 100},
			{l: 600},
			{x: 140, r: 8, l: 1200}
		]);
		
	}, '#newsstand_detail');
	
	
	var ti = setInterval(function() {
		if( robbie.paper != false && squaw != false ) {
			window.clearInterval(ti);
			animateRandomly();
		}
	}, 100);
	
	var banner = $('.banner');
	setInterval(function() {
		banner[ banner.hasClass('lights') ? 'removeClass' : 'addClass' ]('lights');
	}, 300);
	
	
	$('.press .right ul.accordian > li span').live('click', function() {
		$('.right ul.accordian ul').slideUp();
		$(this).next().slideDown();
	});
	
	$('.press .right ul.accordian > li li').live('click', function() {
		$('#content .left').animate({
			scrollLeft: "0px"
		}, 1000);
		$('#popup .left').load('press.html?month=' + $(this).attr('month') + '&year=' + $(this).attr('year') + ' .left > *');
	});
	
	$('.press_link').live('click', function() {
		var data = eval($(this).find('.data').text());
		// console.log( 'http://shop.designasylum.com/gallery_photos/m_ic__' + data[0].id + "." + data[0].extension );
		// console.log($('.press_detail .main_image')[0]);
		//$('.press_detail .main_image').replaceWith('<img class="main_image" style="float: left" src="http://shop.irregularchoice.com/gallery_photos/l_ic__' + data[0].id + "." + data[0].extension  + '" height="400" />');
		
		$('.press_detail ul').empty();
		for( var i = 0; i < data.length; i++) {
//			$('.press_detail ul').append('<li><a href="http://shop.irregularchoice.com/gallery_photos/l_ic__' + data[i].id + "." + data[i].extension + '" target="_blank"><img src="http://shop.irregularchoice.com/gallery_photos/m_ic__' + data[i].id + "." + data[i].extension + '" /><a/></li>')
			if(i == 0) {
				var target = $('.press_detail ul').append('<li><img src="http://shop.irregularchoice.com/gallery_photos/s_ic__' + data[i].id + "." + data[i].extension + '" /></li>').find('img:first');
			} else {
				$('.press_detail ul').append('<li><img src="http://shop.irregularchoice.com/gallery_photos/s_ic__' + data[i].id + "." + data[i].extension + '" /></li>')
			}
		}
		var firstImage = new Image();
		firstImage.onload = function() {
			target.click();
		}
		firstImage.src = 'http://shop.irregularchoice.com/gallery_photos/s_ic__' + data[0].id + "." + data[0].extension;
		$('#content .left').animate({
			scrollLeft: "510px"
		}, 1000);
	});
	
	$('.press_detail ul li img').live('click', function() {
		if( this.width > this.height ) {
			var width = 350;
			var height = (this.height / this.width) * 350;
		} else {
			var height = 400;
			var width = (this.width / this.height) * 400;
		} 
		var mainImage = $('.press_detail .main_image img')[0];
		mainImage.src = this.src.replace('s_', 'l_');
		mainImage.height = height;
		mainImage.width = width;
		
		$(mainImage).parent().attr('href', mainImage.src);
	});
	
	$('.press_detail .main_back').live('click', function() {
		$('#content .left').animate({
			scrollLeft: "0px"
		}, 1000);
		return false;
	})
	
	$('.locations .right h5').live('click', function() {
		$('.scrolling_wrap').animate({
			scrollLeft: ($(this).prevAll('h5').length * 510) + "px"
		}, 1000);
	});
	
	$('.store .group img').live('click', function() {
		$(this).parents('.store').find('.main').attr('src', this.src.replace('s_', 'l_'));
	}).filter(':first').click();
	
});

function animateRandomly() {
	if( (Math.floor( Math.random() * 10 ) % 2) == 0) {
		squaw.animate( array_rand(["leftquick", "leftslow"], 1), false, function() {
			setTimeout(function() {	animateRandomly(); }, 3000 + (2000 * Math.random()));
		} );
	} else {
		robbie.animate( array_rand(["slide", "peek"], 1), false, function() {
			setTimeout(function() {	animateRandomly(); }, 300 + (2000 * Math.random()));
		} );
	}
}

function animateWave() {
	//$('.front_wave').animate({
	//	top: "-=10px"
	//}, 1000);
	//$('.front_wave').animate({
	//	top: "+=10px"
	//}, 1000);
	
	$('.middle_wave').animate({
		top: "+=10px"
	}, 1000);
	$('.middle_wave').animate({
		top: "-=10px"
	}, 1000, animateWave);
}

function animateSticks(stickWrap) {
	$(stickWrap).loop(function() {
		$(this).animate({
			top: "-=20px"
		}, {
			duration: 2000,
			ease: 'easeInExpo'
		});
		$(this).animate({
			top: "+=20px"
		}, {
			duration: 2000,
			ease: 'easeInExpo'
		});
		$(this).find('*').animate({
			left: "+=40px"
		}, {
			duration: 4000,
			ease: 'easeOutExpo',
			queue: false
		});
	}, function() {
		$(this).animate({
			top: "-=20px"
		}, {
			duration: 2000,
			ease: 'easeOutExpo'
		});
		$(this).animate({
			top: "+=20px"
		}, {
			duration: 4000,
			ease: 'easeOutExpo'
		});
		$(this).find('*').animate({
			left: "-=40px"
		}, {
			duration: 4000,
			ease: 'easeInExpo',
			queue: false
		});
	}, 4000);
}
