// JavaScript Document


$(document).ready(function(){

$height = $(window).height();
$(window).bind("resize", sizeWindow);
	
	$("#popup").hide();
	$("#cover").hide();

	$("#leave_comment").click(function(){ 	loadForm("fm"); sizeWindow(); open(); });
	$("#view_comment").click(function(){	loadForm("cm");	sizeWindow(); open(); });
	$("#no_pid").click(function(){			loadForm("er");	sizeWindow(); open(); });

	
	$("#closeBox").click(function(){	close();  });
	$("#cover").click(function(){		close();  });
	
	function loadForm($click){
		// get product id
		$prod_id = $('#product_num').text();		
		$prod_desc = $('#product_desc').text();	
		
		// create urls 
		$iframe_start = '<iframe id="cmt_frame"  src="';
		$iframe_end = '" frameborder="0" height="567" width="538" scrolling="no"></iframe>';
		$form_url = $iframe_start + '../comments/comment_form.php?prod_id=' + $prod_id + '&d=fm&prod_desc=' + $prod_desc + $iframe_end;
		$view_url = $iframe_start + "../comments/comment_form.php?prod_id=" + $prod_id + "&d=cm&prod_desc=" + $prod_desc + $iframe_end;
		$error_test = $iframe_start + "../comments/comment_form.php" + $iframe_end;
		
		if($click == "fm") $url = $form_url;
		if($click == "cm") $url = $view_url;
		if($click == "er") $url = $error_test;
		
		// load into iframe
		$('#pop_content').html($url);
//		$('#cmt_frame').attr({src: $url	});
	}
	
	function open(){	
		$("#cover").show(); // fadein doesnt allow opacity in ie7		
		$("#popup").fadeIn("slow");		
	}
	
	function close(){
		$("#cover").hide();	
		$("#popup").fadeOut("slow");
		$('#pop_content').html("&nbsp;");
			
	}	
	
	function sizeWindow(){
		$height = $(window).height();
		$width = $(window).width();
		
		$top = positionTop($height, 586);
		$left = positionLeft($width, 538);
		$height = cover($height);
		
		$('#popup').css('top', $top);
		$('#popup').css('left', $left);		
		$('#cover').css('height', $height);
	}
	function positionTop($winHeight, $boxHeight){
		
		$winHeight = $winHeight/2;
		$boxHeight = $boxHeight/2;				
		$top = $winHeight - $boxHeight;		
		if($top < 50)
			$top = 50;			
		$top = Math.round($top);
		$top = $top + "px";
		return $top;
	}
	
	function positionLeft($winWidth, $boxWidth){
		
		$winWidth = $winWidth/2;
		$boxWidth = $boxWidth/2;					
		$left = $winWidth - $boxWidth;		
		if($left < 10)
			$left = 10;			
		$left = Math.round($left);
		$left = $left + "px";
		return $left;
	}
	
	function cover($winHeight){
		if($winHeight < 600){ $winHeight = 600; }
		$height = $winHeight + "px";
		return $height;
	}


});

						   