/*
* @author: Leysam Rosario
* @package: Oscommerce
* @description: Leysam mini-box version.
* @version: 0.1
* @thanks: Thanks to me...
*/


function f_scrollTop() {
	return f_filterResults (
	window.pageYOffset ? window.pageYOffset : 0,
	document.documentElement ? document.documentElement.scrollTop : 0,
	document.body ? document.body.scrollTop : 0
	);
}

function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
	n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

function close_mini(id){
	$('#'+id).animate({
			top: '0',
			opacity: 0
		},'slow',function(){
			$(this).hide();
			$('.panel_hider').animate({
					opacity: 0
				},'fast',function(){
					$(this).hide()
			})
	})
		
}

//if document is ready start process...
$(document).ready(function(){
	$('a').click(function(){
														
		var xScroll, yScroll;
		
		$('.close_mini').remove();
				
		if (window.innerHeight&&window.scrollMaxY) {	
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		
		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less than height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
	
		// for small pages with total width less than width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = xScroll;		
		} else {
			pageWidth = windowWidth;
		}
		/*---
			pageWidth - Page width
			pageHeight - Page height
		*/
		
		if_panel = $(this).attr('class');
		container_id = $(this).attr('rel');
		include_file = $(this).attr('href');
		body_height = $('body').css('height');
		url = window.location.href;
		$body = $('body');
		
		div_elem = container_id.replace(/[\]\[]/gi,'');
		
		$('.panel_container').prepend('<div class="close_mini"><img title="Close this window" onclick="close_mini(\''+div_elem+'\')" src="images/close_window.png" /></div>');
		
		/* If class is equal to panel start process */
		if(if_panel == 'panel'){
			//Create the div hider
			$('.panel_hider').remove();
			$('#hider').html('<div class="panel_hider" title="Click to close"></div>');
			
			/* Start Process */
			
			$('.panel_hider').css({height:pageHeight+'px'}).animate({
				opacity: 1
				},'fast',function(){
					
					/*
					 * Start include process
					*/
					
					//if include file is equal to NULL
					if(include_file == ''){
						div_elem = container_id.replace(/[\]\[]/gi,'');
						
						$width = $('#'+div_elem).width();
						
						if(pageWidth > $width){
							$F_width = pageWidth-$width;
						}else{
							$F_width = $width-pageWidth;
						}						
						
						screen_width = document.documentElement.clientWidth;
						screen_height = document.documentElement.clientHeight;
						
						$space = $F_width/2;
						final_top = (screen_height-$('#'+div_elem).height())/6;
						final_top = final_top+f_scrollTop();
						/*
						alert(yScroll+'-'+$('#'+div_elem).height()+'='+final_top+''+f_scrollTop());*/
						
						$('#'+div_elem).css({margin: '0 0 0 0',left : $space+'px',opacity:0, position:'absolute'}).show().animate({
							opacity: 1,
							top: final_top+'px'
						},'slow');
						
					}
					
					if(include_file != ''){
					/*
						below final output
						<div id="minibox-contrainer">
							<div id="final-container">
								// included file (href attr)
							</div>
						</div>
					*/
						is_jpg = include_file.match(/.jpg/gi);
						is_jpeg = include_file.match(/.jpeg/gi);
						is_gif = include_file.match(/.gif/gi);
						is_png = include_file.match(/.png/gi);
						is_html = include_file.match(/.html/gi);
						is_php = include_file.match(/.php/gi);
						
						
						if(is_jpg || is_jpeg || is_gif || is_png){
							
							navigate_code = container_id.replace(/[\}\{]/gi,'');
							a_tag = document.getElementsByTagName('a');
							$array_img = new Array();
							$count=0;
							
							for(i=0; i < a_tag.length; i++){
								if(a_tag[i].rel.replace(/[\}\{]/gi,'') == navigate_code){
									$count++;
									$array_img[$count] = a_tag[i].href;
								}
							}
							
							if($array_img.length > 2){		
							
							current = "";
							
								for(i=1; i < $array_img.length; i++){
									if($array_img[i] == url+include_file){
										current = i;
									}
								}
								
								next = $array_img[current+1];
								
								prev = $array_img[current-1];
								
								if(current > 1 ){
									prev_html = '<a class="miniBox_prev" id="" rel="nav" href="'+prev+'">Prev</a>';
								}else{
									prev_html = '<a class="miniBox_prev" title="Prev photo" title="" id ="disabled_p" href="">Prev</a>';
								}
								
								if(current == $array_img.length-1){
									next_html = '<a class="miniBox_next" id="disabled_r" rel="nav" href="'+next+'">Next</a>';
								}else{
									next_html = '<a class="miniBox_next" id="" title="Next photo" rel="nav" href="'+next+'">Next</a>';
								}
								
								$('#hider').append('<div id="minibox-container"><div id="final-container"><img src="'+include_file+'" /></div> <div id="miniBox-navigation">'+prev_html+' '+next_html+'</div></div>');
								
								/*
									navigate start here
								*/
								
								$('a').click(function(){
																			
										$new_current = '';
										img_to_replace = $(this).attr('href');
										is_nav = $(this).attr('rel');
										
										if(is_nav == 'nav'){
											
											for(i=1;i < $array_img.length;i++){
												if($array_img[i] == img_to_replace){
													$new_current = i;
												}
											}
											
											current = $new_current-current;
											
											
											$('#minibox-container img').animate({
												opacity: 0
											},'slow',function(){
												$('#final-container').css({height:$('#minibox-container img').height(),width:$('#minibox-container img').width()});
												
												$(this).hide().attr('src',img_to_replace).load(function(){		
														$img_height = $('#minibox-container img').height();
														$img_width = $('#minibox-container img').width();
														$('#final-container').animate({
															height: $img_height,
															width: $img_width
														},'fast',function(){
															$('#minibox-container img').show().animate({
																opacity:1
															},'fast')
														})			
														
												})
											});
											
											/*
												"Navigation" 
												@note: just dont mind this area
											*/
											
											if(img_to_replace == $array_img[$array_img.length-1]){
												$('.miniBox_next').attr('rel','');		
													$('.miniBox_next').attr('id','disabled_r');	
											}else{
												$('.miniBox_next').attr('href',$array_img[$new_current+1]);
												$('.miniBox_next').attr('rel','nav');	
												$('.miniBox_next').attr('id','');		
											}
											if($new_current == 1){
													$('.miniBox_prev').attr('rel','');		
													$('.miniBox_prev').attr('id','disabled_p');	
											}else{
												if($array_img[$new_current] > 1){
													$('.miniBox_prev').attr('href',$array_img[1-$new_current]);
													$('.miniBox_prev').attr('rel','nav');
													$('.miniBox_prev').attr('class','');
												}else{
													$('.miniBox_prev').attr('href',$array_img[$new_current-1]);
													$('.miniBox_prev').attr('rel','nav');
													$('.miniBox_prev').attr('id','');
												}
												
											}return false;
										}else{
											return false;
										}
										
								});
								
							/*
								navigate end here
							*/
								
							}else{
								$('#hider').append('<div id="minibox-container"><div id="final-container"><img src="'+include_file+'" /></div></div>');
							}
							
							$width = $('#minibox-container img').width();
							if(pageWidth > $width){
								$F_width = pageWidth-$width;
							}else{
								$F_width = $width-pageWidth;
							}
														
							$space = $F_width/2;							
								
							$('#minibox-container').css({margin: '0 0 0 0',left : $space+'px',padding: '10px',opacity:0, position:'absolute', top: f_scrollTop()+'px'}).show().animate({
								opacity: 1
							},'slow');
							
						}
						if(is_html){
							alert('html is not supported. Please refresh the page');
							return false;
						}
						if(is_php){
							$('#hider').append('<div id="minibox-contrainer"><div id="final-container"><?php include("'+include_file+'"); ?></div></div>');
						}
					}					
					/*
					 * end include process
					*/
				}).click(function(){
					if(include_file == ''){			
					
					$('#'+div_elem).animate({
							top: '0',
							opacity: 0
						},'slow',function(){
							$(this).hide();
							$('.panel_hider').animate({
									opacity: 0
								},'fast',function(){
									$(this).hide()
							})
						})
					
					}else{
						$('#minibox-container').animate({
								top: '0',
								opacity: 0
							},'slow',function(){
								$(this).hide();
								$('.panel_hider').animate({
									opacity: 0
								},'fast',function(){
									$(this).hide();
							})
						});
					}
				});
			
			/* Process end here */
			
			//Stop the loading if "a" is clicked
			return false;
		}
		/* End of conditional statement */
	})
});

function show_minibox(div_elem){
		var xScroll, yScroll;
		
		if (window.innerHeight&&window.scrollMaxY) {	
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		
		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less than height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
	
		// for small pages with total width less than width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = xScroll;		
		} else {
			pageWidth = windowWidth;
		}
		/*---
			pageWidth - Page width
			pageHeight - Page height
		*/
		
		if_panel = $(this).attr('class');
		container_id = $(this).attr('rel');
		include_file = $(this).attr('href');
		body_height = $('body').css('height');
		url = window.location.href;
		$body = $('body');
		
		/* If class is equal to panel start process */
		
			//Create the div hider
			$('.panel_hider').remove();
			$('#hider').html('<div class="panel_hider" title="Click to close"></div>');
			
			/* Start Process */
			
			$('.panel_hider').css({height:pageHeight+'px'}).animate({
				opacity: 0.5
				},'slow',function(){
					
					/*
					 * Start include process
					*/
					//if include file is equal to NULL
						//div_elem = container_id.replace(/[\]\[]/gi,'');
						$width = $('#'+div_elem).width();
						
						if(pageWidth > $width){
							$F_width = pageWidth-$width;
						}else{
							$F_width = $width-pageWidth;
						}						
						
						screen_width = document.documentElement.clientWidth;
						screen_height = document.documentElement.clientHeight;
						
						$space = $F_width/2;
						final_top = (screen_height-$('#'+div_elem).height())/2;
						final_top = final_top+f_scrollTop();
							
						
						$('#'+div_elem).css({margin: '0 0 0 0',left : $space+'px',padding: '10px',opacity:0, position:'absolute'}).show().animate({
							opacity: 1,
							top: final_top+'px'
						},'fast');
						
						
						
				}).click(function(){
						$('#'+div_elem).animate({
								top: '0',
								opacity: 0
							},'fast',function(){
								$(this).hide();
								$('.panel_hider').animate({
									opacity: 0
								},'fast',function(){
									$(this).hide()
							})
						});
						
				});
			
			/* Process end here */
			
			//Stop the loading if a is clicked
			return false;
		/* End conditional statement */
	
}
