﻿/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;

//loading popup with jQuery magic!
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#masthead2").css({
			"opacity": "0.7"
		});
		$("#masthead2").fadeIn("slow");
		$("#popupContact").fadeIn("slow");
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#masthead2").fadeOut("slow");
		$("#popupContact").fadeOut("slow");
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupContact").height();
	var popupWidth = $("#popupContact").width();
	//centering
	$("#popupContact").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	$("#masthead2").css({
		"height": windowHeight
	});
	
}

function insertComment(tweet)
{
	if( document.getElementById ) {// this is the way the standards work
    		div_popup = document.getElementById('popupContact');

    		a_text = unescape(document.getElementById('text_'+tweet).value);
		new_text = document.createElement('p');
		new_text.setAttribute('class', 'status');
//		new_h1 = document.createElement('h1');
//		new_h1.appendChild(document.createTextNode(a_text));
		new_text.innerHTML = '<h1>'+a_text+'</h1>';
//		new_text.appendChild(new_h1);
		div_popup.appendChild(new_text);

		var x = 0;
		var y = 0;
		for (x = 0; x < div_popup.childNodes.length + 1; x++) {
			var close_b = div_popup.childNodes[y];
			if ((close_b.nodeType == 1) && (close_b.getAttribute('id') == 'popupContactClose')) {
				y = y + 1;
			} else
				div_popup.removeChild(close_b);
		}
		a_total = document.getElementById('total_'+tweet).value;
		a_images = unescape(document.getElementById('images_'+tweet).value);
		array_images = a_images.split(';;');
		a_names = unescape(document.getElementById('names_'+tweet).value);
		array_names = a_names.split(';;');
		a_comments = unescape(document.getElementById('comments_'+tweet).value);
		array_comments = a_comments.split(';;');
		for (x = 0; x < a_total; x++) {
			new_p = document.createElement('p');
			new_img = document.createElement('img');	
			new_img.setAttribute('src', array_images[x]);
			new_span = document.createElement('span');
			new_span = document.createTextNode(array_comments[x]);
			new_p.appendChild(new_img);
			new_p.appendChild(new_span);
			div_popup.appendChild(new_p);
		}
		var close_button = document.createElement('a');
		close_button.appendChild(document.createTextNode('x'));
	}
}

//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
	
	//LOADING POPUP
	//Click the button event!
	$("#button_0").click(function(){
		insertComment(0);
		//centering with css
		centerPopup();
		//load popup
		loadPopup();
	});

	$("#button_1").click(function(){
		insertComment(1);
		//centering with css
		centerPopup();
		//load popup
		loadPopup();
	});

	$("#button_2").click(function(){
		insertComment(2);
		//centering with css
		centerPopup();
		//load popup
		loadPopup();
	});

	$("#button_3").click(function(){
		insertComment(3);
		//centering with css
		centerPopup();
		//load popup
		loadPopup();
	});

	$("#button_4").click(function(){
		insertComment(4);
		//centering with css
		centerPopup();
		//load popup
		loadPopup();
	});

				
	//CLOSING POPUP
	//Click the x event!
	$("#popupContactClose").click(function(){
		disablePopup();
	});
	//Click out event!
	$("#masthead2").click(function(){
		disablePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});

  $('#nav').each(function() {
    var $links = $(this).find('a'),
      panelIds = $links.map(function() { return this.hash; }).get().join(","),
      $panels = $(panelIds),
      $panelwrapper = $panels.filter(':first').parent(),
      delay = 500,
      heightOffset = 0;
      
    $panels.hide();
    
    $links.click(function() {
      var link = this, 
        $link = $(this);
      
      if ($link.is('.active')) {
        return false;
      }
      
      $links.removeClass('active');
      $link.addClass('active');
                   
      if ($.support.opacity) {
        $panels.stop().animate({opacity: 0 }, delay);
      }
      
      $panelwrapper.stop().animate({
        height: 0
      }, delay, function () {
        var height = $panels.hide().filter(link.hash).css('opacity', 1).show().height() + heightOffset;
        
        $panelwrapper.animate({
          height: height
        }, delay);
      });

	  
    });
    
    $links.filter(window.location.hash ? '[hash=' + window.location.hash + ']' : ':first').click();
  });
});
