
$(window).load(initiate);

$(window).resize(setupSize);


function setupSize(){
	
    var winW = $(window).width();
    var winH = $(window).height();
	
	$("#body").css("display", "block").css("left", parseInt((winW - 800)/2)).css("top", parseInt((winH - 468)/2));	
}

function initiate(e){
	
  setupSize(); 
 
  $('img[@src$=.png]').ifixpng(); 
  $('div[id^=content]').ifixpng();
  // loopAllPngs();
   bindInteraction();
  

  //getLinks(); 
  

  
  
 }

function bindInteraction(){
	
		$("a:has(img)").bind("mouseenter",function(){
										  
		//path = "./templates/common/images/" + $(this).parent().attr("id");
		//ext=".png";
		
		if(! $(this).children().get(0)){
			return;
		}
		
     	src = $(this).children().get(0).getAttribute("src");
		path = src.substr(0, src.length-4);
		ext = src.substr(src.length-4);
				 
		
		$(this).children().get(0).setAttribute("src", path + "_active" + ext);		
		//$(this).ifixpng();
		
    }).bind("mouseleave",function(){
		if(! $(this).children().get(0)){
			return;
		}
		
		src = $(this).children().get(0).getAttribute("src");
		path = src.substr(0, src.length-11);
		ext = src.substr(src.length-4);
		
		$(this).children().get(0).setAttribute("src", path + ext);	
		//$(this).ifixpng();
		
    });
	
	$("img[src$='_active.png']").each(
										function(){
											$(this).parent().unbind();
											//alert("aaa");
										}
											);
	$(".popup-open").each(
						  function(){
							  var text;
							  switch($(this).attr("id")){
								  case "rate_rev": text = "searching for business reviews written by other members. Posting/editing recommendations."; break;
								  case "friends_fav": text = "searching for business reviews that only friends recommend."; break;	
								  case "advertise": text = "a 7 step business registration form with a lot of user-friendly interactive options (AJAX was applied)."; break;
								  case "admin_panel": text = "A reconstruction of the entire admin panel of this huge project.";break;	
								  default:   text = "";
							  }
							  $(this).bind("mouseenter", function(e){popup(e, text, true);} ).
							  		  bind("mousemove", function(e){popup(e, text, false);} ).
									  bind("mouseleave",
										   		function(e){
													if($("#popup").css("display") == "block"){
														$("#popup-content").empty();
														$("#popup").css("display", "none");														
													}
												}
										   )

						  }
					);
	
	
	$("div.inside-content").each(
							  function(){
							  	H_container = $(this).parent().height();
								H_content   = $(this).height();
								
								var content = $(this);

								if(H_content <= H_container){
									return false;
								}
								
								arrow_down = $('<a href="#" id="arrow-down"><img src="templates/common/images/arrow-down.png" /></a>');
								$(this).parent().prepend(arrow_down);
								arrow_down.click(
											function(){
												scrollContent(content, true);
											});
								
								arrow_up = $('<a href="#" id="arrow-up"><img src="templates/common/images/arrow-up.png" /></a>');
								$(this).parent().prepend(arrow_up);
								arrow_up.click(
											function(){
												scrollContent(content, false);
											});
								
								$(document).mousewheel(function(event, delta) {
									$(document).unmousewheel();
									if (delta > 0){
										scrollContent(content, false);			
									}else{
										scrollContent(content, true);			
									}
								});
								
							});
	
}

function scrollContent(content, direction){
	
	H_container = content.parent().height();
	H_content   = content.height();	
	H_shift = H_container-30; //pixels

	if(direction){ //down		
		newTop = parseInt(content.css("top"))-H_shift;
		if(newTop < -H_content + H_container-5){
			newTop = -H_content + H_container-5;
		}
		
	}else{
		newTop = parseInt(content.css("top"))+H_shift;
		if(newTop > 0){
			newTop = 0;
		}
	}
	
	content.animate({"top": newTop}, 1500, '',
					function(){
						if(newTop < -H_content+H_container){
							$("#arrow-down").css("display", "none")
						}else if($("#arrow-down").css("display") == "none"){
							$("#arrow-down").css("display", "block");							
						}
						
						if(newTop < 0){
							$("#arrow-up").css("display", "block")
						}else if($("#arrow-up").css("display") == "block"){
							$("#arrow-up").css("display", "none");							
						}
						
						$(document).mousewheel(function(event, delta) {
									$(document).unmousewheel();
									if (delta > 0){
										scrollContent(content, false);			
									}else{
										scrollContent(content, true);			
									}
								});

					});		
	
}

function popup(e, text, add_text){
	 //alert($("#popup").height());
	$("#popup").css("left", e.pageX);
	$("#popup").css("top", e.pageY - $("#popup").height());
	if(add_text){														
		$("#popup-content").html(text);
		$("#popup").fadeIn();	
		/*
		$("#popup-close").click(function(){
									$("#popup").fadeOut("slow");
								});	
		*/
	}
}
	function loopAllPngs(){
    var badBrowser = (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");
    if (badBrowser) {
      // get all pngs on page
      $('img[src$=.png]').each(function() {
        if (!this.complete) {
          this.onload = function() { fixPng(this) };
        } else {
          fixPng(this);
        }
      });
    }
  }

 function fixPng(png) {
	 	
   var blank = new Image();
   blank.src = '/images/pixel.gif';
   // get src
   var src = png.src;
   // set width and height
   if (!png.style.width) { png.style.width = $(png).width(); }
   if (!png.style.height) { png.style.height = $(png).height(); }
   // replace by blank image
   png.onload = function() { };
   png.src = blank.src;
   // set filter (display original image)
   png.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
 }

/*
function getLinks(p,s){	
	try{		
		$.ajax({
			   url:'ajaxLinks.php', // relative uri to server 
			   type: 'GET',
			   data: (s ? 's=' + encodeURIComponent(s) + '&' : '') + 'p=' + encodeURIComponent(p),
			   dataType: "html",
			   success: function(html){handleResponse(html, p);}
			   });
	}catch(e){
		alert('Error: ' + e.toString());
	}
}

function handleResponse(responseText){
	$("#body").html(responseText);
	
	$(document).ready(function() {
    var badBrowser = (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");
    if (badBrowser) {
      // get all pngs on page
      $('img[src$=.png]').each(function() {
        if (!this.complete) {
          this.onload = function() { fixPng(this) };
        } else {
          fixPng(this);
        }
      });
    }
  }); 
	
	
	bindInteraction();	
}
*/

