function getViewPort(){
	var viewportwidth;
	var viewportheight;
 
	// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
 
	if (typeof window.innerWidth != 'undefined')
	{
		viewportwidth = window.innerWidth,
		viewportheight = window.innerHeight
	}
 
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)

	else if (typeof document.documentElement != 'undefined'
		&& typeof document.documentElement.clientWidth !=
		'undefined' && document.documentElement.clientWidth != 0)
	{
		viewportwidth = document.documentElement.clientWidth,
		viewportheight = document.documentElement.clientHeight
	}
 
	// older versions of IE
 
	else
	{
		viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
		viewportheight = document.getElementsByTagName('body')[0].clientHeight
	}
	return [viewportwidth, viewportheight];
}

function updateParent(username) {
	dojo.query("INPUT[ name = 'field01' ]").attr("value", username);
	dijit.byId("wishlist").hide();
	return false;
}

function frameLoaded(obj){
	var doc = getIFrameDoc(obj);
	hidePreloader();
	
	dojo.query("FORM", doc).forEach(function(item){
		var onsubmit = item.getAttribute("onsubmit");
		
		if(onsubmit && onsubmit.match("updateParent")){
			dojo.attr(item, "onsubmit", onsubmit.replace(/updateParent/g, "top.updateParent"));
		}
	});
	
	dojo.query("A", doc).forEach(function(item){
		if(!dojo.attr(item, "href").match("/wishlist/")){
			dojo.attr(item, "target", "_top");
		}
	});
	resizeFrame();
}

function showPreloader(){
	var preloader = dojo.query("#wishlist .loading");
	preloader = preloader[0];
	var coords = dojo.coords(preloader.parentNode);
	dojo.style(preloader, {display: "block", height: coords.h + "px", width: coords.w + "px"});
}

function hidePreloader(){
	var preloader = dojo.query("#wishlist .loading");
	preloader = preloader[0];
	dojo.style(preloader, {display: "none"});
}

function getIFrameDoc(obj){
	var doc = obj.contentDocument;
	if (doc == undefined || doc == null)
    	doc = obj.contentWindow.document;
	
	return doc;
}

function wishlistUnloaded(obj){
	console.log("Unload fired");
	showPreloader();
}

function resizeFrame(){
	var realFrameHeight = 0;
	var changeHeightAnim;
	var dialogTop = 0;
	var bodyCoords = getViewPort();
	var frameObj = dojo.byId("wishlistFrame");
	var FrameCurrentHeight = parseInt(dojo.style("wishlistFrame", "height"));
	var DialogCurrentTop = parseInt(dojo.style("wishlist", "top"));
	var doc = getIFrameDoc(frameObj);
	var frameContent = dojo.query("DIV.main", doc)[0];
	
	if(!frameContent) return;
	
	realFrameHeight = parseInt(dojo.style(frameContent, "height") + 15);
	if((realFrameHeight + 50) > bodyCoords[1]) realFrameHeight = bodyCoords[1] - 50;
	changeHeightAnim = dojo.animateProperty({node: "wishlistFrame", delay: 50, properties:{ height: { end: realFrameHeight, unit: 'px' }}});
	if(FrameCurrentHeight > realFrameHeight){
		dialogTop = DialogCurrentTop + (FrameCurrentHeight - realFrameHeight)/2;
	}else{
		dialogTop = DialogCurrentTop - (realFrameHeight - FrameCurrentHeight)/2;
	}
	dojo.animateProperty({node: "wishlist", delay: 50, properties:{ top: { end: dialogTop, unit: 'px' }}}).play();
	changeHeightAnim.play();
}


dojo.addOnLoad(function(){
	dojo.query("A.wishlist").connect("click", function(e){
		e.preventDefault();
		showPreloader();
		dijit.byId("wishlist").show();
		dojo.query("#wishlist iframe").attr("src", dojo.attr(this, "href"));
	});
});
