//$.ui.dialog.defaults.bgiframe = true;

$(document).ready(function(){
		if($('.body-content').length && !$('.body-content').children('.title').children('.without-title-page').length){
			$('.body-content').children('.title').append($('<div class="green-line"><hr /></div>'));
			$('.body-content').children('.title').append($('<div class="icons-bar"><a href="#" class="print"></a><a href="#" class="send-to-friend" href="javascript:;" onclick="popup();"></a></div>'));
			
			$('.green-line').width(Math.abs($('.body-content').children('.title').width())-Math.abs($('.body-content').children('.title').children('h1').width())-Math.abs($('.body-content').children('.title').children('.icons-bar').width()));
		}else{
			
			$('.body-content').children('.title').append($('<div class="icons-bar"><a href="#" class="print"></a><a href="#" class="send-to-friend"></a></div>'));
			
		}
	
});
	
	
function Sendtofriend(){
		
			$("#frm_sendtofriend").find("[name=friendMail],[name=userMail],[name=submit]").attr('disabled',true);
			$('#sendToFriendDialogMessageFailure,#sendToFriendDialogMessagesuccess').hide();
			$('#sendToFriendDialogLoader').show();
					$.ajax({
					url: "index.php?pg=sendtofriend&disable_rewrite=1&plain=1",
					type:"POST",
					async: true,
					dataType:'json',
					data: {
						friendMail:$("#frm_sendtofriend").find("[name=friendMail]").val(),
						userMail  :$("#frm_sendtofriend").find("[name=userMail]").val(),
						cur_url   :document.URL
					},
					beforeSend: function(XMLHttpRequest) {
					},
					success: function(json){
								if(json.err) {
								   $('#sendToFriendDialogMessageFailure').html(json.msg).show();
								}else{
									$('#sendToFriendDialogMessagesuccess').html(json.msg).show();
									$('#sendToFriendDialogLoader').hide();
								    $("#frm_sendtofriend").find("[name=friendMail],[name=userMail],[name=submit]").attr('disabled',false);	
									setTimeout ("closeWin();", 5000 ); 
								}

					},
					error: function (XMLHttpRequest, textStatus, errorThrown) {
						alert('Server Error') ;
						$('#sendToFriendDialogLoader').hide();
						$("#frm_sendtofriend").find("[name=friendMail],[name=userMail],[name=submit]").attr('disabled',false);
					}
				});
		}	
/////////////////////////////////////////////////////////


 function print_content(){
			    
				 /*   var headstr = "<html><head><title>Print</title></head><body style='background:red;background-image:none;'>";
					var footstr = "</body>";
					var newstr = document.getElementById('print_div').innerHTML;
					var oldstr = document.body.innerHTML;
					document.body.innerHTML = headstr+newstr+footstr;
					window.print(); 
					document.body.innerHTML = oldstr;
					return false; */
					
					var prtContent = document.getElementById('print_div');
					var WinPrint =
					window.open('','','left=0,top=0,width=800px,height=500px,toolbar=0,scrollbars=1,status=0');
					WinPrint.document.write(prtContent.innerHTML);
					WinPrint.document.close();
					WinPrint.focus();
					WinPrint.print();
					WinPrint.close();
					prtContent.innerHTML=strOldOne;

			   
	}
	
///////////////////////////////////////////////////				
		
		
		
		
	$(document).ready(function(){
							 	if($('.products-categories-container').length)$('.products-categories-container').slider();
								if($('.products-list-items').length)$('.products-list-items').productsOccourdion();
								if($('.products-list-items').length)$('.products-list-items').buildMediaGallery();
							 });
		
		
	$.fn.extend({
				
					productsOccourdion:function(){
						$(this).children('li').each(
													function(index, element){
														$(this).children('a').click(function(){
																							 //$(this).next('div').toggle('slow');
				
																			$(this).parent().nextAll().each(function(index, element){$(this).children('div').slideUp("slow", function(){$(this).prev('a').removeClass('active');});});
																			$(this).parent().prevAll().each(function(index, element){$(this).children('div').slideUp("slow", function(){$(this).prev('a').removeClass('active');});});
																							 																			 																			$(this).next('div').slideDown("slow", function(){$(this).prev('a').addClass('active');});
																							 });
														
														
														});
						if(document.location.href.indexOf('#')!=-1){
							var activeElement = document.location.hash.replace("#", "");
							$(this).children("li:has(a[href$='#"+activeElement+"'])").children("a").click();
						}else {
							$(this).children('li:first').children("a").click();
						}
					},				
					
					
				buildMediaGallery:function(){
						var DWidth 	= 700;
						var DHeight = 500;
						$(this).find('.media-item').each(
														 function(index, element){
															 	$(this).click(function(){
																	if(!$('#mediaShadow').length){
																			$(document.body).append("<div id='mediaShadow'></div>");
																			$('#mediaShadow').click(
																				function(){
																					$(this).remove();$('#mediaPlayer').remove();
																				}
																			);
																			
																	}
																	var width  	= $(document.body).width();
																	var height 	= $('#mediaShadow').height();
																	var left 	= (width 	- DWidth) /2;
																	var top 	= (height	- DHeight) /2;
																	var player 	= $(this).attr("player");
																	var src 	= $(this).attr("src");
																	var image	= $(this).attr("image");
																	
																	if(!$('#mediaPlayer').length){
																		$(document.body).append("<div id='mediaPlayer' style='left:"+left+"px; top:"+top+"px; width:"+DWidth+"px; height:"+DHeight+"px; '></div>");
																	}
																	
																	var s1 = new SWFObject(player,'player', DWidth ,DHeight,'9');
																		s1.addParam('allowfullscreen','true');
																		s1.addParam('allowscriptaccess','always');
																		s1.addParam('wmode','transparent');
																		s1.addParam('flashvars','file='+src+"&image="+image+"&autostart=true");
																		s1.write('mediaPlayer');
																	
																	
																	
																	
																});
															 
															 }
														 
														 );
						
					},		
					
					
					slider:function(){
						new slider().init(
								   	{
										element : $(this) 	,
										speed	: 500
									}
								  );
					}
				
				});	
		
		
		
		/* SLIDER */
	function slider(){
		
		this.element 	= '';
		this.interval	= null;
		this.delay		= 2000;
		this.speed	 	= 1000;
		this.step		= 0;
		this.leftArrow	= '';
		this.rightArrow	= '';
		this.itemsContainer= '';
		this.DMarginLeft= 0;
		
		
		var me = this;
		
		
		this.init 		= function(settings){
			this.element 		= settings.element;
			this.speed			= settings.speed != undefined ? settings.speed : this.speed;
			this.itemsContainer	= $(this.element).find('ul');
			this.leftArrow		= $(this.element).find('.left-arrow');
			this.rightArrow		= $(this.element).find('.right-arrow');			
			this.step			= this.itemsContainer.children('li').width();


			if(this.itemsContainer.children('li').length > 3){
				this.leftArrow.click(function(){	me.moveLeft();	});
				this.rightArrow.click(function(){  	me.moveRight();	});			
				this.start();
			}
			
		}
		
		this.start 		= function(){
			this.interval = setTimeout(function(){me.moveLeft();}, me.delay);
		}
		
		this.stop 		= function(){
			clearTimeout(me.interval);
		}
		
		this.moveLeft 	= function(){			
			this.stop();
			this.itemsContainer.children('li:first').stop().animate({marginLeft:"-"+me.step+"px"},me.speed, function(){
				$(this).appendTo(me.itemsContainer).css({marginLeft:me.DMarginLeft+'px'}); me.start(); 
																											  });
		}
		
		this.moveRight 	= function(){
			this.stop();
			this.itemsContainer.children('li:first').before(me.itemsContainer.children('li:last').css({marginLeft:"-"+me.step+'px'}));
			this.itemsContainer.children('li:first').animate({marginLeft:"+"+me.DMarginLeft+"px"}, me.speed, function(){ me.start();});
		}
		
		return this;
				
	}
	
	
	
	
		$(document).ready(function () {  
            if($('.print').length)$('.print').click(function(){print_page();});
           // if user clicked on button, the overlay layer or the dialogbox, close the dialog    
			 $('#dialog-overlay').click(function () {       
				 $('#dialog-overlay, #dialog-box').hide();         
				 return false;  
			 });  
       
			 // if user resize the window, call the same function again  
			 // to make sure the overlay fills the screen and dialogbox aligned to center      
			 $(window).resize(function () {      
					 //only do it if the dialog box is not hidden  
					 if (!$('#dialog-box').is(':hidden')) popup();  
		            $('.send-to-friend').click(function(){popup();});  
             });   
      });  
   
	 function closeWin(){
		  $('#dialog-box ,#dialog-overlay').hide(); 
	 }

	 //Popup dialog  
	 function popup() {  
		 // get the screen height and width    
		 var maskHeight = $(document).height();    
		 var maskWidth = $(window).width();  
		   
		 // calculate the values for center alignment  
		 var dialogTop =  (maskHeight/3) - ($('#dialog-box').height());    
		 var dialogLeft = (maskWidth/2) - ($('#dialog-box').width()/2);   
		 
		// assign values to the overlay and dialog box  
		 $('#dialog-overlay').css({height:maskHeight, width:maskWidth}).show();  
		 $('#dialog-box').css({top:dialogTop, left:dialogLeft}).show();  
		   
		 // display the message  
		 $('#dialog-message').html('<h1 align="center" style="clear:both;">Send to friend'+
								   '<a href="javascript:return false" onclick="closeWin()">'+
								   '<img src="'+g_templatepath+'/cdn/ecaru/images/closeWin.png" align="right" />&nbsp;</a>'+
								   '</h1>'+
								   '<form name="frm_sendtofriend" id="frm_sendtofriend" method="POST" onSubmit="Sendtofriend();return false;" >'+
								   '<p style="clear:both;">'+
								   '<label>Friend email</label><input type="text" name="friendMail" width="10px"  />'+
								   '</p>'+
								   '<p style="clear:both;">'+
								   '<label>Your email</label><input type="text" name="userMail"  />'+
								   '</p>'+
								   '<p style="clear:both;">'+
								   '<input type="submit" name="submit" value="submit" class="button"/>'+
								   '</p>'+
								   '<br />'+
								   '<div id="sendToFriendDialogMessageFailure"></div>'+
								   '<div id="sendToFriendDialogMessagesuccess"></div>'); 
		 
	 } 
	
	




	function print_page(){
			
					var disp_setting= "toolbar=no,location=no,directories=no,menubar=no,scrollbars=yes,width=900, height=900, left=0, top=0"; 
					var content_value= $('.content').html(); 
					var docprint=window.open("","",disp_setting); 
					docprint.document.open(); 
					docprint.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>Entag </title><link rel="stylesheet" type="text/css" href="'+g_templatepath+'/cdn/ecaru/css/style.css" media="all" />');				
					docprint.document.write('</head><body onLoad="self.print()" style="margin:20px 20px 20px 20px;"><center><div><div style="width:100%;margin-left:0px; clear:left;float:left;"><img src="'+g_templatepath+'/cdn/ecaru/images/27.png" align="left"/></div><div style="clear:left;float:left;">');          
					docprint.document.write('<table class="print-page"><thead></thead><tfoot valign="bottom" ></tfoot><tbody><tr><td class="page-tbody-container">'+content_value+'</td></tr></tbody></table></div>');          
					docprint.document.write('</div></center></body></html>'); 
					 
					docprint.focus(); 
					docprint.document.close();
	}
		

	
	

