var vacancies = new Vacanies();



/**
 * Vacanies
 * @extends Page
 * @constructor
 */
function Vacanies()
{
	// Call the prototype's constructor
	Page.call(this);

	// Private
	var self			= this,
		dom				= new DOM();

	// Public
	this.addPrintButtonShops 		= addPrintButtonShops;
	this.addPrintButtonCoolblue 	= addPrintButtonCoolblue;

	//load on pageload
	self.addEventListener(Page.LOADED, addPrintButtonShops, false);
	self.addEventListener(Page.LOADED, addPrintButtonCoolblue, false);

	/**
	 * Show the print button on the product page
	 */
	function addPrintButtonShops()
	{
		if(!dom.getById("layout_content"))
		{
			return false;
		}
		if(!dom.getById("layout_content").getByClassName("vacancy", "div"))
		{
			return false;
		}

		var vacancyItems = dom.getById("layout_content").getByClassName("vacancy", "div");

		// Create iframe
		function createIframe() {
			if (window.frames["print_frame"]) {
				return window.frames["print_frame"];
			}
	
			var iframe	= document.createElement("iframe"),
				body	= dom.getFirstByTagName("body", document.documentElement);
	
			iframe.id	= "print_frame";
			iframe.name	= "print_frame";
			iframe.src	= "about:blank";
	
			body.appendChild(iframe);
			
			return window.frames["print_frame"];
		}

		for (var i = 0; i < vacancyItems.length; i++)
		{
			var vacancyId = vacancyItems[i].id;
			var sectionfooter = dom.getById(vacancyId).getByClassName("sectionfooter", "p");

			//create link
			var a = dom.create(
				"a",
				{
					href		: "#",
					title		: "Print deze vacature",
					className 	: "js",
					onclick 	: (function(vacancyId) {
							return function() 
							{
								return printItem(vacancyId);
							}
						}
					
					)(vacancyId)
				},
				[
					"Print deze Vacature",
					dom.create("br")
				]
			)
			sectionfooter[0].insertBefore(a,sectionfooter[0].firstChild);
		}

		function printItem(vacancyId)
		{
			createIframe();
			var links = document.getElementsByTagName("link");
			var stylesheets = "";
			for (var i = 0; i < links.length; i++) {
				if (links[i].type == "text/css") {
					stylesheets += "<link rel=\"stylesheet\" type=\"text/css\" src=\""+links[i].href+"\"/>";
				}
			}
			try
			{
				var iframe = dom.getById("print_frame");
				var content = dom.getById(vacancyId).innerHTML;
				var iframeDocument = (iframe.contentWindow || iframe.contentDocument);
				if (iframeDocument.document)
				{
					iframeDocument = iframeDocument.document;
				}
				iframeDocument.write("<html><head><title>Vacature</title>");
				iframeDocument.write(stylesheets);
				iframeDocument.write("</head><body onload='this.focus(); this.print();'>");
				iframeDocument.write(content + "</body></html>");
				iframeDocument.close();
			}
			catch(e)
			{
				self.print();
			}
			return false;
		}
	}



	/**
	 * Show the print button on the product page
	 */
	function addPrintButtonCoolblue()
	{
		if(!dom.getById("right"))
		{
			return false;
		}
		if(!dom.getById("right").getByClassName("block"))
		{
			return false;
		}

		var vacancyItems = dom.getById("right").getByClassName("block");

		// Create iframe
		function createIframe() {
			if (window.frames["print_frame"]) {
				return window.frames["print_frame"];
			}
	
			var iframe	= document.createElement("iframe"),
				body	= dom.getFirstByTagName("body", document.documentElement);
	
			iframe.id	= "print_frame";
			iframe.name	= "print_frame";
			iframe.src	= "about:blank";
	
			body.appendChild(iframe);
			
			return window.frames["print_frame"];
		}

		for (var i = 0; i < vacancyItems.length; i++)
		{
			var vacancyId = vacancyItems[i].id;
			var sectionfooter = dom.getById(vacancyId).getByClassName("printvacancy", "p");

			//create link
			var a = dom.create(
				"a",
				{
					href		: "#",
					title		: "Print deze vacature",
					className 	: "js",
					onclick 	: (function(vacancyId) {
							return function() 
							{
								return printItem(vacancyId);
							}
						}
					
					)(vacancyId)
				},
				[
					"Print deze Vacature",
					dom.create("br")
				]
			)
			sectionfooter[0].insertBefore(a,sectionfooter[0].firstChild);
		}

		function printItem(vacancyId)
		{
			createIframe();
			var links = document.getElementsByTagName("link");
			var stylesheets = "";
			for (var i = 0; i < links.length; i++) {
				if (links[i].type == "text/css") {
					stylesheets += "<link rel=\"stylesheet\" type=\"text/css\" src=\""+links[i].href+"\"/>";
				}
			}
			try
			{
				var iframe = dom.getById("print_frame");
				var content = dom.getById(vacancyId).innerHTML;
				var iframeDocument = (iframe.contentWindow || iframe.contentDocument);
				if (iframeDocument.document)
				{
					iframeDocument = iframeDocument.document;
				}
				iframeDocument.write("<html><head><title>Vacature</title>");
				iframeDocument.write(stylesheets);
				iframeDocument.write("</head><body onload='this.focus(); this.print();'>");
				iframeDocument.write(content + "</body></html>");
				iframeDocument.close();
			}
			catch(e)
			{
				self.print();
			}
			return false;
		}
	}
}

// Inheritance
Vacanies.prototype = new Page();
controller.setPageObject(vacancies);