﻿$(function(){  
	
	// setup ajax history.
	$.history.init(displayContent);
  
	/* attach content loading module */
	$("a[href*='#cms']").click(function(){	
		// rebuild cms query.
		var qHref = $(this).attr('href');
		var nQuery = qHref.substring(qHref.lastIndexOf('cms'), qHref.length);
		$.history.load(nQuery);			
		return false;		
	});
	/* attach content loading module */
	
	/* attach event search content */	
	$(".search-box").keypress(function(e){			
		if(e.keyCode=='13'){
			var searchQuery = 'cms:search:' + $(this).val();
			$.history.load(searchQuery);			
			return false;			
		}				
	});	
	$(".ui-searchbutton").click(function(){
		if($('.ui-searchtextbox').val() == ''){
			alert('Vui lòng nhập nội dung cần tìm kiếm.');
			return false;
		}else{
			var searchQuery = 'cms:search:' + $(".ui-searchtextbox").val();
			$.history.load(searchQuery);			
			return false;
		}
		return false;
	});
	/* attach event search content */
});

function displayContent(contentid){

	// check does this request ajax cms.
	if(contentid.lastIndexOf('cms') == -1) return false;
	
	// get content display contener.
	var displayContener = $('.cms');
	// create ajax loading panel.
	var ajaxLoading = "";
	ajaxLoading += "<div class='widget' style='width: 100%; height: 100%'>";
	ajaxLoading += "<div class='bl'>";
	ajaxLoading += "<div class='tr'>";
	ajaxLoading += "<div class='tl'>";
	ajaxLoading += "<div class='widget-body'>";
	ajaxLoading += "<div class='content-loading'><p><img border='0' align='absmiddle' src='/PublicFolder/Image/Loading.gif'/></p><br/><p>Đang tải dữ liệu từ máy chủ, xin vui lòng đợi trong vài giây.</p></div>";
	ajaxLoading += "</div>";				
	ajaxLoading += "</div>";
	ajaxLoading += "</div>";
	ajaxLoading += "</div>";
	ajaxLoading += "</div>"
	
	if(displayContener != null){
		
		// scroll to top of the current page.
		window.scrollTo(0,0);
		// clear all content in container.			
		displayContener.html(ajaxLoading);		
		
		// query string
		var reqQuery = "";
			
		if(contentid.lastIndexOf("search") != -1)
		{   			
			reqQuery = "/widgetServices.aspx?ac=scms&sf="+ encodeURIComponent(contentid.split(':')[2]);					
		}
		else
		{
			var cmsQuery = contentid.split('|')[1];			
			
			// slipt get content id.
			var contentId = "";
			var categoryId = "";
			
			if(cmsQuery.lastIndexOf(':') != -1){
				contentId = cmsQuery.split(':')[1];
				categoryId = cmsQuery.split(':')[2];
			}else{
				contentId = cmsQuery;
			}
			
			reqQuery = "/widgetServices.aspx?ac=lcms&cms="+ contentId + ((categoryId != "")? "&cat="+ categoryId : "");
			
		}
		
		// ajax call to load cms display control.
		displayContener.load(
			reqQuery,
			function(){
				
				// if the content request form search then attach datatable plugin to paging the result.
				var tablePagingOption = {
					rowsPerPage: 10,
					firstArrow: (new Image()).src="/PublicFolder/Image/firstpg.gif",
					firstArrowDisable: (new Image()).src="/PublicFolder/Image/firstpgd.gif",
					prevArrow: (new Image()).src="/PublicFolder/Image/prevpg.gif",
					prevArrowDisable: (new Image()).src="/PublicFolder/Image/prevpgd.gif",
					lastArrow: (new Image()).src="/PublicFolder/Image/lastpg.gif",
					lastArrowDisable: (new Image()).src="/PublicFolder/Image/lastpgd.gif",
					nextArrow: (new Image()).src="/PublicFolder/Image/nextpg.gif",
					nextArrowDisable: (new Image()).src="/PublicFolder/Image/nextpgd.gif"
				}
				if($(".content-search-result") != null)
					$(".content-search-result").tablePagination(tablePagingOption);
				
				/* resize thumbnail images in listview mode */
				$('.content-icons img[class!=nomodify]').each(function(){
					// declare newwidth newheight
					var newWidth = 85;
					var newHeight = 85;
					var maxHeight = 85;					
					
					if($(this).width() <= newWidth)
						newWidth = $(this).width();
						
					newHeight = $(this).height() * newWidth / $(this).width();
					if(newHeight > maxHeight)
					{
						newWidth = $(this).width() * maxHeight / $(this).height();
						newHeight = maxHeight;
					}
					
					if( $(this).width() > newWidth || $(this).height() > newHeight){
						// apply these changed.
						$(this).width(newWidth);	
						$(this).height(newHeight);
					}					
				});
				/* resize thumbnail images in listview mode */
				
				/* attach function to display content image resize with image title */
				$('.content-viewer-body img[class!=noneresize][class!=nomodify]').each(function(){
					
					// declare newwidth newheight
					var newWidth = 400;
					var newHeight = 0;
					var maxHeight = 400;					
					
					if($(this).width() <= newWidth)
						newWidth = $(this).width();
						
					newHeight = $(this).height() * newWidth / $(this).width();
					if(newHeight > maxHeight)
					{
						newWidth = $(this).width() * maxHeight / $(this).height();
						newHeight = maxHeight;
					}
					
					if( $(this).width() > newWidth || $(this).height() > newHeight){
						$(this).attr('title', "nhấn để xem hình với kích thước ( "+ $(this).width() +"x"+ $(this).height()  +" )");
						$(this).click(function(){
							$(this).colorbox({href: $(this).attr('src')});	
						});
						
						// apply these changed.
						$(this).width(newWidth);	
						$(this).height(newHeight);
					}				
					// styling the images.	
					$(this).css('border','solid 1px #dedede');
					$(this).css('padding','4px');
					$(this).css('cursor','pointer');
					$(this).css('-moz-border-radius','8px');
				});				
				
				// attach even click on each content navigate
				$("a[href*='#cms']]").live('click', function(){					
					// rebuild cms query.
					var qHref = $(this).attr('href');
					var nQuery = qHref.substring(qHref.lastIndexOf('cms'), qHref.length);
					$.history.load(nQuery);
					// ignore default click event.
					return false;
				});
				
				// attach event print content.
				$("a[class='content-print']").live('click', function(){
					
					// get content display area.
					var printContentHtml = $(".printarea").html();						
					
					// create print popup window.
					var printWnd = window.open("","ContentPrint","height=600,width=800,status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=0,scrollbars=1,");
					printWnd.document.write("<html><head><title>Prin Preview</title>");
					printWnd.document.write("<link rel='stylesheet' href='/Templates/LIBHUI/css/cms-print.css'>");
					printWnd.document.write("</head><body>");
					printWnd.document.write("<input type='button' value='Print' onclick='javascript:window.print();self.close()'/><input type='button' value='Close' onclick='javascript:self.close()'/>");
					printWnd.document.write("<br/>");
					printWnd.document.write(printContentHtml);
					printWnd.document.write("<p>&nbsp;</p>");
					printWnd.document.write('</body></html>');
					printWnd.document.close();
					return false;
									
				});
			}
		);
	}	
}

