﻿// These are produced by Your-IT-Business-Solutions.com you need there permission to copy
// . = Class
// # = Id
var PdfFileToPrint   = '';
var PrintPdfMsgSw    = 'N';
var PrintPdfErrMsgSw = 'N';
var DownloadAdobe    = '';
  	 

var YourItBS={
  // Just tests if you can write to the webpage
	WriteSome:function(myText){
		document.write('<div>\n');
		document.write(myText); //generate CSS to hide contents
		document.write('<\/div>');
	}
	,
  // This will align objects justified across the total width of the parent object "row_justified".
  // In the main row object you need the class="row_justified" and l_pad="##" c_pad="##" r_pad="##"> (## between 0 and 1)
  // Created 20101003 BRW
	JustifyObjs:function(e){
	  var parent = $('div.'+e);
	  var kids = parent.children().size();
	  var l_pad = parseFloat(parent.attr("l_pad"));
	  var c_pad = parseFloat(parent.attr("c_pad"));
	  var r_pad = parseFloat(parent.attr("r_pad"));
		var sumFactors = l_pad + r_pad + (c_pad*(kids-1));
		
		var totalRowWidth = parent.outerWidth();
	  var totalObjWidth = 0;
	  
		parent.children().each(function (ctr) {
		  var child = $(this);
		  child.css("display", "inline-block"); // if you don't do this it might take the whole line
		  totalObjWidth = totalObjWidth + child.outerWidth();
		})
	  var totalRemainingWidth = totalRowWidth - totalObjWidth;

		parent.children().each(function (ctr) {
		  var child = $(this);
		  if (ctr == 0)
		  { child.before('<div style="display:inline-block; width:'+((totalRemainingWidth * l_pad)/sumFactors)+'px"></div>'); }
		  else
		  { child.before('<div style="display:inline-block; width:'+((totalRemainingWidth * c_pad)/sumFactors)+'px"></div>'); }
		  if (ctr == (kids-1))
		  { child.after('<div style="display:inline-block; width:'+((totalRemainingWidth * r_pad)/sumFactors)+'px"></div>'); }
		})
	}
	,
	// Adds the "New" flag gif on the end of any element that has children that have a tag flags='NewFlag'
	NewFlags:function(e){
	  var parent = $('div.'+e);
		parent.children().each(function (ctr) {
		  var child = $(this);
		  if ( child.attr('flags') == 'NewFlag' )
		  { child.after('<img class="New" src="images/New!.300.png" border="none" width="24" align="top" alt="New!"/>'); }
		})
	}
	,
	// Sets the "Print PDF" button to display or hide based on if it has the "pdf" attribute.
	// When users click on the "Print PDF" button it will then load the PDF and bring up the Print Dialog box. 
	// Also hides the PDF frame, which would only be shown if you had a PDF load issue last time
	SetPDF:function(e){
		$('a').each(function (ctr){ 
      var e = $(this);
      
		  e.click(function(event){
				$("#printFrame").hide();
				if ( e.attr('pdf') )
				{ 		
					$('#PrintPdfBtn').show();
			    if (e.attr('pdf') == '_')
		      // Short cut in case the files are pathed and named the same except ".full.jpg"
		      {	PdfFileToPrint = e.attr('href').replace(/.full.jpg/i, ".pdf"); }
		      else
		      { PdfFileToPrint = e.attr('pdf'); }
		    }
		    else
		    {
		      $('#PrintPdfBtn').hide();
		      PdfFileToPrint = ''; 
		    }
			});
		})
	}
	,
	DetectPlugins:function() {
 	  //checks if browser object exists
		if (typeof $.browser === "undefined" || !$.browser) {
			var browser = {};
			$.extend(browser);
		}
		var pluginList = {
			pdf: {
				activex: ["acroPDF.PDF.1", "PDF.PdfCtrl.1", "PDF.PdfCtrl.4", "PDF.PdfCtrl.5", "PDF.PdfCtrl.6"],
				plugin: /adobe\s?acrobat/gim
			}
		};
		var isSupported = function (p) {
			if (window.ActiveXObject) {
				$.browser[p] = false;
				
				for (i = 0; i < pluginList[p].activex.length; i++) {
					try {
						new ActiveXObject(pluginList[p].activex[i]);
						$.browser[p] = true;
					} catch (e) {}	
				}
			} else {
				$.each(navigator.plugins, function () {
					if (this.name.match(pluginList[p].plugin)) {
						$.browser[p] = true;
						return false;
					} else {
						$.browser[p] = false;
					}
				});
			}
		};
		
		$.each(pluginList, function (i, n) {
			isSupported(i);
		});

		if (!jQuery.browser.pdf)
		{
      var qstn = confirm('You seem not to have Adobe Reader installed on your browser or\n'
                        +'you have the "Display PDF in browser" not checked.\n'
                        +'\n'
						            +"To print PDF documents you will need this plugin.\n"
						            +'\n'
						            +'Click "OK" to download it.\n'
						            +'Click "Cancel" to not download it.');
      if (qstn)
      {
				window.frames.Iframe1.location.href="http://get.adobe.com/reader/";
				DownloadAdobe = 'YES';
			}
		}
	}
	,
	// ========================================================================================================
	// Shows the current status of the cookies and allows you to clear it.
	// Requires:  <div class="ShowCookies" title="GetPdfReader"></div>
	// Nice2Have: .ShowCookies{font-size:20px;} .ShowCookies a{font-size:16px;}
	// ========================================================================================================
	ShowCookies:function(e){
	  $('.ShowCookies').each(function (ctr) {
		  var CookieName = $(this).attr('title');
		  $(this).html(CookieName+'='+getCookie(CookieName)+' <a>clear</a>');
		  $(this).find('a').click(function(){
		    setCookie(CookieName, '', -1);
		    location.reload();
			})
		})
  }
}



//================================================================================================================
// Functions to set attributes/actions on objects
//================================================================================================================
// PrintPdfButton (Add onclick event)
$(function(){
	$('#PrintPdfBtn').click(function(){
    DownloadAdobe = '';
    YourItBS.DetectPlugins();
    if ((DownloadAdobe != 'YES') && (jQuery.browser.pdf))
    {
	    // This works on IE
	    $("#PdfWrapper").html('<embed style="position:fixed; width:1px; height:1px; right:0px; top:50px" id="printFrame" src="'+PdfFileToPrint+'" type="application/pdf" autostart="true" width="320" height="260">').show();
			// This only works on IE the first time you try to print a given PDF, the next time it won't work
	  	//$("#PdfWrapper").html(
	    //  '<object id="printFrame" data="'+PdfFileToPrint+'" type="application/pdf" width="300" height="200" style="position:fixed; width:1px; height:1px; right:0px; top:50px">'+
	    //  '  <embed style="position:fixed; width:1px; height:1px; right:0px; top:50px" id="printFrame2" src="'+PdfFileToPrint+'" type="application/pdf" autostart="true" width="320" height="260">'+
	    //  '</object>'
	  	//  ).show();
	 		 	
	  	setTimeout(function(){
	 	 		var thePdf = $("#printFrame")[0];
	 	 		if (PrintPdfMsgSw == 'N' && PdfFileToPrint != '')
	  	 	{ alert('Remember to set Print Range: ALL'); PrintPdfMsgSw = 'Y'; }
				
				try{ 
				  thePdf.print();              // Dialog Box
				  //thePdf.printWithDialog();  // Dialog Box
				  //thePdf.printAll();         // No Dialog Box, prints all pages
				  //thePdf.printAllFit(true);  // No Dialog Box, prints all pages, and shrinks
				} catch(e) { 
				  if (PrintPdfErrMsgSw == 'N')
					{
						alert('You will have to manually right-click and select "print.." on the little PDF'); 
						/*
						alert('There was an issue with the PDF Print Dialog Box opening. \n'+
						      'You will have to manually right-click and select "print.." on the little PDF\n\n\n'+
						      'Information for the webmaster:\n'+
						      e); 
						*/
						PrintPdfErrMsgSw = 'Y';
			    }
			    $("#printFrame").css({width:"150px", height:"170px"});
				} 
	  	}, 1000);
			return(true);
		}
 	});
})

jQuery(document).ready(function($){
  YourItBS.JustifyObjs('row_justified');
  YourItBS.NewFlags('submenu');
  YourItBS.SetPDF('submenu');
  //alert('appName='+navigator.appName+', appCodeName='+navigator.appCodeName+',appMinorVersion='+navigator.appMinorVersion
  //     +',appVersion='+navigator.appVersion+', cookieEnabled='+navigator.cookieEnabled+', platform='+navigator.platform
  //     +', plugins='+navigator.plugins+', userAgent='+navigator.userAgent);
})



function mylog(e, msg) {
	for (var i in e)
	{ msg += i + "=" + e[i] + "\n"; }
	alert('Write out: '+msg);
}

function getCookie(Name){ 
		var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair
		if (document.cookie.match(re)) //if cookie found
			return document.cookie.match(re)[0].split("=")[1] //return its value
		return null
}

function setCookie(name, value, days){
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = '; expires='+date.toGMTString();
	}
	else
	{	var expires = ''; }
	document.cookie = name+'='+value+expires+";path=/";
} 

function eraseCookie(name) {
	setCookie(name,'',-1);
}




