/* Author: no4design.com */
$(function() {		
	// initialize scrollable with mousewheel support
	$(".highlights").scrollable({ vertical: true });
	$(".people").scrollable({ vertical: true, mousewheel: true });
	$(".partners").scrollable({ });
	
	$("nav.preview a").each(function(index){
		$(this).mouseover(function(){
			clearMenu();
			if ( $('div.highlights div.default').css('display') == 'block' ) {
				$('div.highlights div.default').fadeOut();
				$(".highlights div.items").fadeIn();
			}
			var api = $(".highlights").data("scrollable");
			api.seekTo(index);
			$(this).addClass("active");
		});
	});
	
	function clearMenu(){
		$("nav.preview a").each(function(index){
			$(this).removeClass("active");
		})
	}
	
	function clearPeople(id){
		$('#first-name').html("");
		$('#last-name').html("");
		$('#position').html("");
		$('#company').html("");
		$('#quote').html("");
		$('#portraits img').each(function(){
			if ($(this).attr("id") != id){
				$(this).fadeOut();
			}
		});
	}
	
	$('div.people a').each(function(){
		
		$(this).hover(
		function(){
			$(this).children(".over").fadeIn();
		},
		function(){
			if (!$(this).hasClass('selected')) $(this).children(".over").fadeOut();
		});
		
		$(this).click(function(){
			clearPeople( $(this).attr('id') );
			
			$('div.people a').each(function(){
				$(this).removeClass("selected");
			});
			$(this).addClass("selected");
			
			$.ajax({
			   type: "GET",
			   url: url,
			   data: "api=employee&pid="+$(this).attr("rel"),
			   dataType: "json",
			   success: function(msg){
					$('#first-name').html(msg.fname);
					$('#last-name').html(msg.lname);
					$('#position').html(msg.position);
					$('#company').html(msg.company);
					$('#quote').html(msg.quote);
					
					if ( $('#i-'+msg.id).length > 0 ){
						$('#i-'+msg.id).fadeIn();
					} else {
						var img = new Image();
						$(img).load(function () {
					        $(this).css('display', 'none');
					        $(this).hide();
							$("#portraits").append(this);
							$(this).fadeIn();
					    }).error(function () {
					    }).attr('src', msg.src).attr("id", "i-"+msg.id);
					}	
			   }
			 });
			
		});
	});
	
	function clearPartners(id){
		$('#partner-name').html("");
		$('#partner-link').html("");
		$('#partner-entry').hide();
		$('#partner-entry').html("");
		$('#partner-logos img').each(function(){
			if ($(this).attr("id") != id){
				$(this).fadeOut();
			}
		});
		if ( $('div.client-gallery').length > 0 ){
			clearTimeout(ct);
			cg = -1;
			$('div.client-gallery div.gallery').html("");
			/*
			$('div.client-gallery div.gallery').hide({
				complete: function(){
					$(this).html("");
				}
			});
			*/
		}
	}
	$('#partners a').each(function(){
		
		$(this).hover(
		function(){
			$(this).children(".over").fadeIn();
		},
		function(){
			if (!$(this).hasClass('selected')) $(this).children(".over").fadeOut();
		});
		
		$(this).click(function(){
			clearPartners( $(this).attr('id') );
			
			$('div.partners a').each(function(){
				$(this).removeClass("selected");
			});
			$(this).addClass("selected");
			
			$.ajax({
			   type: "GET",
			   url: url,
			   data: "api=partner&pid="+$(this).attr("rel"),
			   dataType: "json",
			   success: function(msg){
					$('#partner-name').html(msg.name);
					$('#partner-entry').html(msg.content);
					$('#partner-entry').show()
					$('#partner-link').html(msg.link);
					
					if ( $('#i-'+msg.id).length > 0 ){
						$('#i-'+msg.id).fadeIn();
					} else {
						var img = new Image();
						$(img).load(function () {
					        $(this).css('display', 'none');
					        $(this).hide();
							$("#partner-logos").append(this);
							$(this).fadeIn();
					    }).error(function () {
					    }).attr('src', msg.src).attr("id", "i-"+msg.id);
					}
					
					if ( $('div.client-gallery').length > 0 ){
						$('div.client-gallery div.gallery').html(msg.gallery);
						$('div.client-gallery div.gallery').show();
						clientGallery();
					}
			   }
			 });
		});
	});
	
	$("a.fancybox").fancybox();
	$("a.iframe").fancybox({
		'hideOnContentClick': true,
		'width': 940,
		'height': 575,
		'margin': 60,
		'padding': 0,
		'scrolling' : 'no',
		'autoScale' : false,
		'overlayColor': '#000'
	});
	
	$("a.popup").click(function(){
		$("#"+$(this).attr('rel')).fadeIn();
	});
	
	var cg = -1;
	var ct;
	var cl = 3000;
		
	function clientGallery(){
		cg++;
		if (cg > ($("div.client-gallery div.gallery img").length -1)) cg = 0;
		$("div.client-gallery div.gallery img").each(function(index){
			if ( cg == index ){
				$(this).fadeIn({
					complete: function(){
						ct = setTimeout(clientGallery, cl);
					}
				});
			} else {
				if ($(this).css("display") != "none") $(this).fadeOut();
			};
			
			
		});
	}
	clientGallery();
	
	$("#post-form a.submit").click(function(){
		var $inputs = $("#post-form :input");
	    var values = {};
	    var error = false;
	    var that = this;
	    str = new Array();
	    var captcha;
	    $inputs.each(function() {
	    	if (this.name == "ccaptcha") captcha = $(this);
	        if ($(this).val() != "") {
	        	str.push(this.name+"="+$(this).val());
	        	$(this).removeClass("error");
	        } else {
	        	switch( this.name ){
	        		case "ccomment":
	        		case "cname":
	        		case "cemail":
	        		case "ccaptcha":
	        			$("#"+this.id).addClass("error");
	        			error = true;
	        		break;
	        	}
	        }
	    });
	    str = str.join("&");
		var pid = $(this).attr("rel");
	    
    	$.ajax({
		   type: "GET",
		   url: url+"/api/captcha/"+captcha.val(),
		   data: str,
		   dataType: "json",
		   success: function(msg){
		   		if (msg.result) {
		   			if (str && !error){
					    $.ajax({
						   type: "GET",
						   url: url,
						   data: "api=postsave&pid="+pid+"&"+str,
						   dataType: "json",
						   success: function(msg){
						   		$("#the-wall #post-box").after(msg.html);
								$("#post-"+msg.id).show("blind", {}, 500);
								$inputs.not(':button, :submit, :reset, :hidden').val('').removeAttr('checked').removeAttr('selected');
								$("#post-ctype").val("");
								$("#post-cfile").val("");
								$("#post-uploads span.text").html("").hide();
								$("#post-uploads").parent().children("div.photo").css("visibility", "visible");
								$("#post-uploads").parent().children("div.video").css("visibility", "visible");
								$inputs.each(function(){
									$(this).removeClass("error");
								});
					   			newCaptcha('post-captcha');
								triggerComments();
						   }
						 });
					 }
		   		} else {
		   			captcha.addClass("error");
	        		newCaptcha('post-captcha');
		   		}
		   }
		 });
	});
	function initCaptcha(){
		$("a.code").click(function(){
			newCaptcha( $(this).attr("rel") );
		});
	}
	initCaptcha();
	function newCaptcha(id){
		var id = id;
		$.ajax({
		   type: "GET",
		   url: url+"/api/captcha/new",
		   dataType: "json",
		   success: function(msg){
		   	$( "#"+id+" input:first").val(msg.prefix);
		   	$("#"+id+" img").attr("src", msg.image);
		   }
		 });
	}
	
	
	$("a.delete.postd").click(function(){
		var that = this;
		$.ajax({
		   type: "GET",
		   url: url,
		   data: "api=postdelete&pid="+$(this).attr("rel"),
		   dataType: "json",
		   success: function(msg){
				var id = "#post-"+$(that).attr("rel");
				$(id).hide("blind", { 
					direction: "vertical",
					complete: function(){
						$(this).remove();
					}
					
				}, 500);
		   }
		 });
	});
	$("a.delete.commentd").click(function(){
		var that = this;
		$.ajax({
		   type: "GET",
		   url: url,
		   data: "api=commentdelete&pid="+$(this).attr("rel"),
		   dataType: "json",
		   success: function(msg){
				var id = "#comment-id-"+$(that).attr("rel");
				var parent = $(id).parent();
				var la = parent.children("article:last");
				$(id).hide("blind", { 
					direction: "vertical",
					complete: function(){
						$(this).remove();
						la = parent.children("article:last")
						if (la.length == 0) parent.addClass("hidden");
					}
					
				}, 500);
				if (!la.hasClass("nborder")) la.addClass('nborder');
				
		   }
		 });
	});
	
	function clearCommentBox(selected){
		$("a.comment").each(function(){
			if ($(this).attr("rel") != selected) $(this).removeClass('active');;
		});
		$("div.commentbox").each(function(){
			var that = this;
			if ((selected+"-box") != $(this).attr("id")){
				$(this).hide("blind", {
					"complete": function(){
						if ($(that).parent().children("article").length == 0) $(that).parent().addClass("hidden");
						$(this).remove();
					}
				}, 500);
			}
		});
	}
	
	function triggerComments(){
		$("a.comment").click(function(){
			var that = this;
			clearCommentBox($(this).attr("rel"));
		
			if ( !$(this).hasClass('active') ) {
				$.ajax({
				   type: "GET",
				   url: url,
				   data: "api=comment&pid="+$(this).attr("rel"),
				   dataType: "json",
				   success: function(msg){
				   		$( "#"+$(that).attr("rel") ).removeClass("hidden");
						$( "#"+$(that).attr("rel") ).append(msg.comment);
						$("#"+$(that).attr("rel")+"-box").removeClass("hidden");
						$("#"+$(that).attr("rel")+"-box").show("blind", {
							complete: function(){
								//window.location = "#"+$(that).attr("rel")+"-box";
							}
						}, 500);
								
						createUpload($(that).attr("rel")+"-uploads", "photo-ph-"+$(that).attr("rel"), "video-ph-"+$(that).attr("rel"));
						initCaptcha();
//SUBMIT BUTTON
						$( "#"+$(that).attr("rel")+ " a.submit" ).click(function(){
							var form = $(this).attr("rel").split("-box");
							var $inputs = $("#"+form.join("")+"-form :input");
						    var values = {};
						    var error = false;
						    str = new Array();
						    $inputs.each(function() {
	    						if (this.name == "ccaptcha") captcha = $(this);
						        if ($(this).val() != "") {
						        	str.push(this.name+"="+$(this).val());
						        	if ($("#"+this.id).length > 0) $("#"+this.id).removeClass("error");
						        } else {
						        	switch(this.id){
						        		case $(that).attr("rel")+"-ccomment":
						        		case $(that).attr("rel")+"-cname":
						        		case $(that).attr("rel")+"-cemail":
						        		case $(that).attr("rel")+"-ccaptcha":
						        			$("#"+this.id).addClass("error");
						        			error = true;
						        		break;
						        	}
						        }
						    });
						    
						    str = str.join("&");						    
							var pid = $(this).attr("rel");
						    
					    	$.ajax({
							   type: "GET",
							   url: url+"/api/captcha/"+captcha.val(),
							   data: str,
							   dataType: "json",
							   success: function(msg){
							   		if (msg.result) {
							   			
							   			if (str && !error){
									    	pid = pid.split("comment-").join("");
										    $.ajax({
											   type: "GET",
											   url: url,
											   data: "api=commentsave&pid="+pid+"&"+str,
											   dataType: "json",
											   success: function(msg){
													$("#"+$(that).attr("rel")+" article").each(function(){
														$(this).removeClass("nborder");
													});
													$("#"+$(that).attr("rel")).append(msg.html);
											   		$( "#"+$(that).attr("rel") ).removeClass("hidden");
													$("#comment-id-"+msg.id).show("blind", {}, 500);
													clearCommentBox();
													$("a.fancybox").fancybox();
											   }
											 });
										 }
							   			
							   		} else {
							   			captcha.addClass("error");
						        		newCaptcha($(that).attr("rel")+'-captcha');
							   		}
							   }
							 });
						  
						});
				   }
				 });
			 }
			$(this).addClass('active');
			
		});
	}
	
	//
	
	if ( $("#s-analysis-and-review").length > 0 ) {
		$("#s-analysis-and-review").show("fade", {
			complete: function(){
				$("#s-strategic-planning").show("fade", {
					complete: function(){
						$("#s-organize-and-media-relation").show("fade", {
							complete: function(){
								$("#s-control-and-coordinate").show("fade", {}, 700);
							}
						}, 700);
					}
				}, 700);
			}
		}, 700);
	
	}
	
	
	triggerComments();
	
	window.onload = function(){
		createUpload("post-uploads", "photo-ph-post", "video-ph-post");
	};
	
	function createUpload(uid, pid, vid){
		var swfphoto;
		var swfvideo;
		
		var settings = {
			flash_url : url+"/wp-content/themes/brandmarkt/js/libs/swfupload/swfupload.swf",
			upload_url: url+"/api/upload/photo",
			file_size_limit : "1 MB",
			file_types : "*.jpg;*.gif;*.png;*.jpeg;",
			file_types_description : "Web Images",
			file_upload_limit : 1,
			file_queue_limit : 0,
			debug: false,
			moving_average_history_size: 40,
			// Button settings
			button_image_url: url+"/wp-content/themes/brandmarkt/images/button-photos.png" ,
			button_width: "30",
			button_height: "22",
			button_placeholder_id: pid,
			button_color: "#FF0000",
			// The event handler functions are defined in handlers.js
			file_queued_handler : fileQueued,
			file_dialog_complete_handler: fileDialogComplete,
			upload_start_handler : uploadStart,
			upload_progress_handler : uploadProgress,
			upload_success_handler : uploadSuccess,
			upload_complete_handler : uploadComplete,
			custom_settings : {
				cid : uid,
			}
		};
		swfphoto = new SWFUpload(settings);
		var settings = {
			flash_url : url+"/wp-content/themes/brandmarkt/js/libs/swfupload/swfupload.swf",
			upload_url: url+"/api/upload/video",
			file_size_limit : "20 MB",
			file_types : "*.flv;*.f4v;*.m4v;*.mp4;*.mov",
			file_types_description : "Web Video",
			file_upload_limit : 1,
			file_queue_limit : 0,
			debug: false,
			moving_average_history_size: 40,
			// Button settings
			button_image_url: url+"/wp-content/themes/brandmarkt/images/button-videos.png" ,
			button_width: "30",
			button_height: "22",
			button_placeholder_id: vid,
			// The event handler functions are defined in handlers.js
			file_queued_handler : fileQueued,
			file_dialog_complete_handler: fileDialogCompleteVideo,
			upload_start_handler : uploadStart,
			upload_progress_handler : uploadProgress,
			upload_success_handler : uploadSuccess,
			upload_complete_handler : uploadComplete,
			custom_settings : {
				cid : uid,
			}
		};
		swfvideo = new SWFUpload(settings);
	}
	function fileQueued(file) {
		try {
		} catch (ex) {
			this.debug(ex);
		}
	
	}
	function fileDialogComplete() {
		$("#"+this.customSettings.cid+"-ctype").val("photo");
		this.startUpload();
	}
	function fileDialogCompleteVideo() {
		$("#"+this.customSettings.cid+"-ctype").val("video");
		this.startUpload();
	}
	
	function uploadStart(file) {
		try {
			this.customSettings.progressCount = 0;
			$("#"+this.customSettings.cid).parent().children("div.photo").css("visibility", "hidden");
			$("#"+this.customSettings.cid).parent().children("div.video").css("visibility", "hidden");
			$("#"+this.customSettings.cid).removeClass("hidden");
			updateDisplay.call(this, file);
		}
		catch (ex) {
			this.debug(ex);
		}
	}
	
	function uploadProgress(file, bytesLoaded, bytesTotal) {
		var percent = Math.round((bytesLoaded/bytesTotal)*100);
		$("#"+this.customSettings.cid+" div.bar").width(percent+"%");
		$("#"+this.customSettings.cid+"span.text").html( percent+"%" );
		try {
			this.customSettings.progressCount++;
			updateDisplay.call(this, file);
		} catch (ex) {
			this.debug(ex);
		}
		
	}
	
	function uploadSuccess(file, serverData) {
		try {
			$("#"+this.customSettings.cid+" span.text").html( file['name'] );
			$("#"+this.customSettings.cid+" div.bar").width("0");
			$("#"+this.customSettings.cid+"-cfile").val(serverData);
		} catch (ex) {
			this.debug(ex);
		}
	}
	function uploadComplete(file) {}
	function updateDisplay(file) {}
	
	if ( $("#video-player").length > 0 ){
		$("video").each(function(){
			id = "#"+$(this).attr("id");
			$(id).mediaelementplayer();
		});
	} else {
		$("video").each(function(){
			id = "#"+$(this).attr("id");
			$(id).mediaelementplayer({
				mode:'shim',
				videoWidth : 400,
				videoHeight : 300,
		    defaultVideoWidth : 400,
		    defaultVideoHeight : 300
			});
		});
	}
});




















