/**
 * 
 * 		title:		MAIN - 8PM
 *
 *		author: 	Ovidiu Stefancu
 *					http://www.wpworks.net
 *					info@wpworks.net
 *
 * 		info:		- weblocation.it	
 * 		test		
 */



var WPW = WPW || {};


//SETTINGS
var grid_lines = 4;
var grid_block_width = 320;
var grid_block_height = 400;
//

WPW.collection;
var myScroll;

WPW.Engine = function(){
	WPW.console(screen.width + " < ");
	var main_node = $('#main');

	if($.browser.msie){
		WPW.IE = true;	
		if($.browser.version < 9) WPW.IE8 = true;
		if($.browser.version < 8) WPW.IE7 = true;
	}
	
	
	// VARS
	var main = this;
	var collection = $('#collection');
	WPW.collection = collection;
	var items = [];
	var fancy_array = [];
	
	//
	
	var thumb_width = 0;
	var thumb_height = 0;
	
	//POLAROID OBJECT
	var imageItem = function(thumb){
		var obj = this;
		thumb.detach();
		obj.width = 0;
		obj.height = 0;
		var index = items.length;		
		
		
		var polaroid = $('<div class="polaroid_block"></div>');
		obj.polaroid = polaroid;
		polaroid.hide();
		
		var polaroid_content = $('<div class="polaroid_content"></div>');
		polaroid.append(polaroid_content);

		
		var new_thumb = $(new Image());
		new_thumb.load(function(){
			polaroid_content.append(new_thumb);
			polaroid.fadeIn('slow');
			obj.width = new_thumb.width();
			obj.height = new_thumb.height();
			
			polaroid_content.css('width', new_thumb.width());
			polaroid_content.css('height', new_thumb.height());
			
		}).attr('src', thumb.attr('src'));
		
		polaroid.data('data', obj);
		collection.append(polaroid);

		var info = $('<div class="polaroid_title"></div>');
		obj.info = info;
		info.html(thumb.attr('title'));
		polaroid.append(obj.info);
		
		//POSITION

		
		
		var column = parseInt(index/grid_lines);
		var row = index - (column*grid_lines);
		
		var r_x = Math.random()*grid_block_width*0.2;
		var r_y = Math.random()*grid_block_height*0.2;
		
		polaroid.css("left", column * grid_block_width + r_x + "px");
		polaroid.css("top", row * grid_block_height + r_y + "px");
		
		///
		//		VIDEO MANAGEMENT
		///
		var video_on = false;
		var video = false;
		var video_paths = [];
		var video_type="";
		var video_init = false;
		var video_meta = false;
		var mouse_over = false;
		
		if(thumb.attr("data-video") && WPW.TOUCH_DEVICE == false && !WPW.IE8 && !WPW.IE7){
			mouse_over = true;
			if(video_on == false){
				if(video == false){
					video = '<video class="polaroid_video" poster="'+ thumb.attr('src') +'">';
					if(thumb.attr('data-video')){
						video_paths = thumb.attr('data-video').split('|');
						for(var i = 0; i<video_paths.length; i++){
							var video_path = video_paths[i];
							video_type="";
							if(video_path.search('.mp4') != -1)video_type = 'type="video/mp4"';
							if(video_path.search('.ogg') != -1)video_type = 'type="video/ogg"';
							var v_src = '<source src="'+ video_path +'" '+ video_type +' >';
							video += v_src;
						}
						
						//VIDEO EVENTS
					}
					video += "</video>";
					
					if(video_init == false){
						video = $(video);
						video_init = true;
						video.bind('playing', function(e){});
						video.bind('waiting', function(e){});
						video.bind('progress', function(e){});
						video.bind('error', function(e){});
						video.bind('ended', function(e){
								if(mouse_over)video[0].play();
							});
						video.bind('loadstart', function(e){});						
						video.bind('loadedmetadata', function(e){
								video_meta = true;
								showVideo();
							});						
					}
					
				}
				polaroid_content.append(video);
				
				// video.attr('width', obj.width);
				// video.attr('height', obj.height);
				
				//video.hide();
				if(video_meta == true)showVideo();
				

				video_on = true;
				video[0].play();				
			}
		}

		
		/*
		
		polaroid.mouseleave(function(){
			mouse_over = false;
			if(video_on == true){
				if(video != false){
					video.fadeOut("slow", function(){
						video_on = false;
						video[0].pause();
						video.detach();
					});
					
				}
				
			}
		});
		*/
		
		var showVideo = function(){
			if (video != false) {
				video.fadeOut(0);
				video.fadeIn('slow');
				new_thumb.fadeOut('slow');
			}
		}
		
		///
		//		VIDEO MANAGEMENT - END
		///		


		//zoom button
		
		var btn_zoom = $('<div class="btn_zoom"></div>');
		polaroid.append(btn_zoom);
		
		if(WPW.TOUCH_DEVICE == true)polaroid.click(function() {
			openFancyItem(index);
		});
		
		btn_zoom.click(function() {
			openFancyItem(index);
		});
		
		items.push(this);
		fancy_array.push({
				'href'	: thumb.attr('alt'),
				'title'	: thumb.attr('title')
		});
	}
	//POLAROID OBJECT - END


	var openFancyItem = function(the_index){
		$.fancybox(fancy_array, {
			'index'			: the_index,
			'padding'		: 0,
			'margin'		: 70,
			'overlayColor'	: '#ffffff',
			'overlayOpacity': 1,
			'titleShow'		: true,
			'titlePosition' : 'inside',
		    'transitionIn'	: 'fade',
		    'transitionOut'	: 'fade',
			'centerOnScroll': false,
			'overlayShow'	: true,
			'showNavArrows'	: true,
			'showCloseButton': true,
			'onComplete' 	: addFancyToolbar,
			'onClosed' 		: resetHash,
			'titleFormat'	: formatTitle
		});
	}
	
	$('img', collection).each(function(index){
		var item = $(this);
		var ii = new imageItem(item);
	});
	
	$('img', '#intro_page').each(function(index){
		var thumb = $(this);
		
		var obj = this;
		thumb.detach();
		obj.width = 0;
		obj.height = 0;
		var index = items.length;		
		
		
		var polaroid = $('<span class="polaroid_block"></span>');
		obj.polaroid = polaroid;
		polaroid.hide();
		
		var polaroid_content = $('<span class="polaroid_content"></span>');
		polaroid.append(polaroid_content);

		
		var new_thumb = $(new Image());
		new_thumb.load(function(){
			polaroid_content.append(new_thumb);
			polaroid.fadeIn('slow');
			obj.width = new_thumb.width();
			obj.height = new_thumb.height();
		}).attr('src', thumb.attr('src'));
		
		polaroid.data('data', obj);
		$("#intro_page").prepend(polaroid);

		var info = $('<div class="polaroid_title"></div>');
		obj.info = info;
		info.html(thumb.attr('title'));
		polaroid.append(obj.info);
		
		///
		//		VIDEO MANAGEMENT
		///
		var video_on = false;
		var video = false;
		var video_paths = [];
		var video_type="";
		var video_init = false;
		var video_meta = false;
		var mouse_over = false;
		
		if(thumb.attr("data-video") && WPW.TOUCH_DEVICE == false)polaroid.mouseenter(function(){
			mouse_over = true;
			if(video_on == false){
				if(video == false){
					video = '<video class="polaroid_video" poster="'+ thumb.attr('src') +'">';
					if(thumb.attr('data-video')){
						video_paths = thumb.attr('data-video').split('|');
						for(var i = 0; i<video_paths.length; i++){
							var video_path = video_paths[i];
							video_type="";
							if(video_path.search('.mp4') != -1)video_type = 'type="video/mp4"';
							if(video_path.search('.ogg') != -1)video_type = 'type="video/ogg"';
							var v_src = '<source src="'+ video_path +'" '+ video_type +' >';
							video += v_src;
						}
						
						//VIDEO EVENTS
					}
					video += "</video>";
					
					if(video_init == false){
						video = $(video);
						video_init = true;
						video.bind('ended', function(e){
								if(mouse_over)video[0].play();
							});
						video.bind('loadstart', function(e){});						
						video.bind('loadedmetadata', function(e){
								video_meta = true;
								showVideo();
							});						
					}
					
				}
				polaroid_content.append(video);
				
				video.attr('width', obj.width);
				video.attr('height', obj.height);
				
				video.hide();
				if(video_meta == true)showVideo();
				
				
				video_on = true;
				video[0].play();				
			}
		});
		
		
		
		polaroid.mouseleave(function(){
			mouse_over = false;
			if(video_on == true){
				if(video != false){
					video.fadeOut("slow", function(){
						video_on = false;
						video[0].pause();
						video.detach();
					});
					
				}
				
			}
		});
		
		var showVideo = function(){
			if (video != false) {
				video.fadeIn('slow');
			}
		}
		
		///
		//		VIDEO MANAGEMENT - END
		///		
		
	});


	var columns = parseInt(items.length / grid_lines) + 1;
	
	var collection_width = columns * grid_block_width + parseInt(grid_block_width/2);
	var collection_height = grid_lines * grid_block_height + parseInt(grid_block_height/2);
	
	
	collection.css('width', collection_width + 'px');
	collection.css('height', collection_height + 'px');
	
	//
	// MOUSE DRAG
	//
	
	var oldMx = 0;
	var oldMy = 0;
	var mX = 0;	
	var mY = 0;	
	
	var collection_x = 0;
	var collection_y = 0;
	
	collection.mousedown(function(e){
			oldMx = e.pageX;
			oldMy = e.pageY;
			collection.mousemove(function(e){
				mX = -oldMx + e.pageX;
				oldMx = e.pageX;
				mY = -oldMy + e.pageY;
				oldMy = e.pageY;
				
				collection_x += mX;
				collection_y += mY;
				
				WPW.moveCollection();

				return false;
			});
			
			$('html, body, window').mouseup(function(){
				collection.unbind('mouseup');
				collection.unbind('mousemove');
				$('html, body, window').unbind('mouseup');
				return false;
			});
			
			collection.mouseup(function(){
				collection.unbind('mouseup');
				collection.unbind('mousemove');
				$('html, body, window').unbind('mouseup');
				return false;
			});
			return false;
		});
	
	WPW.moveCollection = function(){
		if(collection_x > 0)collection_x = 0;
		if(collection_x < -collection_width + WPW.windowWidth) collection_x = -collection_width + WPW.windowWidth;
		
		if(collection_y > 0)collection_y = 0;
		if(collection_y < -collection_height + WPW.windowHeight) collection_y = -collection_height + WPW.windowHeight;
		
		collection.css('left', collection_x + "px");
		collection.css('top', collection_y + "px");
	}
	
		
	//FANCY TOOLBAR
	var fancy_toolbar = false;
	var btn_prev;
	var btn_next;
	var btn_close;	
	function addFancyToolbar(){
		if (fancy_toolbar == false) {
			fancy_toolbar = $('<div id="fancy_toolbar"></div>');
			btn_prev = $('<div id="fancy_btn_prev">PREV</div>');
			btn_next = $('<div id="fancy_btn_next">NEXT</div>');
			btn_close = $('<div id="fancy_btn_close">CLOSE</div>');
			
			fancy_toolbar.append(btn_prev);
			fancy_toolbar.append(btn_next);
			fancy_toolbar.append(btn_close);
		}
		btn_next.click(function(){$.fancybox.next();});
		btn_prev.click(function(){$.fancybox.prev()});
		btn_close.click(function(){$.fancybox.close()});		
		
		$('#fancybox-title').prepend(fancy_toolbar);
	}
	
	
	//CHECK SELECTED IMAGE
	
	if(window.location.hash.length > 2){
		var spl =  window.location.hash.split('_');
		if(spl.length > 0){
			openFancyItem(spl[1] - 1);
		}
	}
	
	function formatTitle(title, currentArray, currentIndex, currentOpts) {
		window.location.hash = "!pic_" + (currentIndex + 1);
		var facebook_url = 'http://www.facebook.com/sharer.php?u=' + location.href + '&t=' + '8PM Collection';
		facebook_url = facebook_url.replace('#', '%23');
		facebook_url = facebook_url.replace('!', '&#33');
		
		setTimeout(addFancyToolbar, 200);
		
	    return '<div id="fancy_fb_share">' + '<a href="'+facebook_url+'">SHARE ON FACEBOOK</a>' + '</div>';
	}

	function resetHash(){
		window.location.hash = "";
	}		


	//ARROW CONTROLS
	
	
	WPW.stepMoveCollection = function(h_move, v_move){
		var tx = h_move * grid_block_width;
		var ty = v_move * grid_block_height;
		
		collection_x +=tx;
		collection_y +=ty;
		
		if(collection_x > 0)collection_x = 0;
		if(collection_x < -collection_width + WPW.windowWidth) collection_x = -collection_width + WPW.windowWidth;
		
		if(collection_y > 0)collection_y = 0;
		if(collection_y < -collection_height + WPW.windowHeight) collection_y = -collection_height + WPW.windowHeight;
		
//		collection.css('left', collection_x + "px");
//		collection.css('top', collection_y + "px");
		
		
		collection.animate({
					left: collection_x,
					top: collection_y
				}, {
					duration: 600,
					easing: "easeInOutQuad",
					step: function(){},
					complete: function(){}
				});
		
		
	}	
	
	
	
	var arrow_controls = $('<div id="arrow_controls"></div>');
	var arrow_btn_up = $('<div id="arrow_up"></div>');
	var arrow_btn_down = $('<div id="arrow_down"></div>');
	var arrow_btn_left = $('<div id="arrow_left"></div>');
	var arrow_btn_right = $('<div id="arrow_right"></div>');
	var arrow_icon = $('<div id="arrow_icon"></div>');
	var arrow_info = $('<div id="arrow_info"></div>');
	
	
	
	
	
	arrow_controls.append(arrow_btn_up);
	arrow_controls.append(arrow_btn_down);
	arrow_controls.append(arrow_btn_right);
	arrow_controls.append(arrow_btn_left);
	arrow_controls.append(arrow_icon);
	arrow_controls.append(arrow_info);
	
	if(collection.length > 0)$('body').append(arrow_controls);

	arrow_info.hide();
	arrow_info.delay(1500).fadeIn("slow");
	arrow_info.delay(6000).fadeOut("fast", function(){arrow_info.detach()});

	
	arrow_btn_up.click(function(){WPW.stepMoveCollection(0, 1)});
	arrow_btn_down.click(function(){WPW.stepMoveCollection(0, -1)});
	arrow_btn_left.click(function(){WPW.stepMoveCollection(1, 0)});
	arrow_btn_right.click(function(){WPW.stepMoveCollection(-1, 0)});


	var oldMx = 0;
	var oldMy = 0;
	var mX = 0;
	var mY = 0;

	function gestureEvent(e){
		
		
	}
	
//	collection.bind("gesturestart", function(e){
//		WPW.console(e.scale);
//	});
//	collection.bind("gesturechange", function(e){
//		WPW.console(e.scale);
//	});
//	collection.bind("gestureend", function(e){
//		WPW.console(e.scale);
//	});
	
	
	if(false)collection.bind("touchstart", function(e){
		var touch2 = e.originalEvent.touches[0] || e.originalEvent.changedTouches[0];
		oldMx = touch2.pageX;
		oldMy = touch2.pageY;
		
		collection.bind("touchmove", function(e){
//			
//			e.preventDefault();
  			var touch = e.originalEvent.touches[0] || e.originalEvent.changedTouches[0];
//			mX = -oldMx + touch.pageX;
//			mY = -oldMy + touch.pageY;
//			oldMx = touch.pageX;
//			oldMy = touch.pageY;

			WPW.console(window.scale);

//			collection_x +=mX;
//			collection_y +=mY;
//			
//			if(collection_x > 0)collection_x = 0;
//			if(collection_x < -collection_width + WPW.windowWidth) collection_x = -collection_width + WPW.windowWidth;
//			
//			if(collection_y > 0)collection_y = 0;
//			if(collection_y < -collection_height + WPW.windowHeight) collection_y = -collection_height + WPW.windowHeight;
//
//			collection.css('left', collection_x + "px");
//			collection.css('top', collection_y + "px");
//
//			return false;
		});	
		
		collection.bind("touchend", function(e){
			collection.unbind('touchmove');
			collection.unbind('touchend');
			e.preventDefault();
			return false;
		});
		collection.bind("touchcancel", function(e){
			collection.unbind('touchmove');
			collection.unbind('touchend');
			e.preventDefault();
			return false;
		});
	});
	
	
	$.fancybox.center = function(){WPW.console('center')}
}





WPW.windowResize = function(){
	var wd = $(window);
	WPW.windowWidth = wd.width();
	WPW.windowHeight = wd.height();
	
	if(WPW.moveCollection)WPW.moveCollection();
}


WPW.TOUCH_DEVICE = false;

$(document).ready(function($){
	WPW.body = $('body');
	
	//DEVICE DETECTION
	var device_patterns = [/iPad/i,/iPod/i,/iPhone/i];
	
	for (var d_i = 0; d_i < device_patterns.length; d_i ++){
		if(navigator.userAgent.match(device_patterns[d_i])){
			WPW.TOUCH_DEVICE = true;
			break;
		} else {
			WPW.TOUCH_DEVICE = false;	
		}
	} 
	
	var ua = navigator.userAgent.toLowerCase();
	if(ua.match(/android/i)) {
		WPW.TOUCH_DEVICE = true;
	}

	
//	$('body').bind("touchmove", function(e){
//	    e.preventDefault();
//	});
	
	WPW.console(WPW.TOUCH_DEVICE);
	
	WPW.site = new WPW.Engine();
	$(window).resize(function() {
	 	WPW.windowResize();
	});
	WPW.windowResize();
	
	$('#collection').css('display', 'block');
	
	
	$('#menu_btn_philosophy').fancybox({
				'padding'		: 0,
				'margin'		: 31,
				'overlayColor'	: '#fff',
				'overlayOpacity': 1,
				'titleShow'		: false,
				'showNavArrows'	: false
	});
	$('#menu_btn_about').fancybox({
				'padding'		: 0,
				'margin'		: 31,
				'overlayColor'	: '#fff',
				'overlayOpacity': 1,
				'titleShow'		: false,
				'showNavArrows'	: false
	});
	$('#menu_btn_contact').fancybox({
				'padding'		: 0,
				'margin'		: 31,
				'overlayColor'	: '#fff',
				'overlayOpacity': 1,
				'width'			: 400,
				'titleShow'		: false,
				'showNavArrows'	: false
	});
	
	
	
	$.fancybox.center = function(){};
});



WPW.console = function($msg, $break, $clear){
	if($("#console").length > 0){
		var the_console = $("#console"); 

	 	if($clear)the_console.html("");
		if(the_console.html().length > 1000)the_console.html("");
		var _break = " | ";
		if($break)_break = "<br />"
	 	//the_console.html(the_console.html() + $msg + _break);
	}
}

