var JQ = jQuery.noConflict();

JQ(document).ready(function(){
    TAFItem.startFunction();
});

var TAFItem = {
    tafWindow :null,
    tafFrame  :null,
    tafButton :null,
    
    startFunction: function(){
    	if(typeof(tafData) != 'undefined') {
	        this.createFrameAndDiv(null);
	        this.attachEvents();
	        this.restoreData();
        }
    },
    
    createFrameAndDiv:function(parentElement)
    {
        var TAFFrame = JQ('<iframe></iframe>');
        TAFFrame.attr('id','TAFFrame');
        TAFFrame.css('height',this.getWindowHeight());
        TAFFrame.hide();
        
        var RTAFC = JQ('<div></div>');
        RTAFC.attr('id','RTAF');
        RTAFC.hide();
        var wrapperParent;
        if(parentElement!=null)
            wrapperParent = JQ(parentElement).parent();
        else
            wrapperParent = JQ('#wrapper').parent();
        TAFFrame.css('height',document.body.clientHeight);
            wrapperParent.append(TAFFrame);
            wrapperParent.append(RTAFC);
        
        this.tafWindow=JQ('#RTAF');
        this.tafFrame=JQ('#TAFFrame');
        this.tafButton=JQ('#tellAFriendButton');
        this.tafWindow.css('opacity',0);
        this.tafFrame.css('opacity',0);
    },
    
    restoreData:function()
    {
        if(tafData.showTAFPopupOnLoad)
            this.showTAFPopup();
    },
    
    dialogEvents: function(){
        var parentThis = this;
        if(this.tafWindow.find('.closeTAFPopupButton').length > 0){
            this.tafWindow.find('.closeTAFPopupButton').click(function(){
                parentThis.closeTAFPopUp();
            });
        }
        if(this.tafWindow.find('#tafSendEmailButton').length > 0){
            this.tafWindow.find('#tafSendEmailButton').click(function(){
                parentThis.TAFSendEmail();
            });
        }
    },
   
    TAFSetFocus: function(){
	if(this.tafWindow.find('.closeTAFPopupButton').length > 0){
            this.tafWindow.find('.closeTAFPopupButton').focus()
	}

        if(this.tafWindow.find('#TAFTo').length > 0) {
                this.tafWindow.find('#TAFTo').focus();
        }

    }, 
    attachEvents: function(){
        var parentThis = this;
        this.tafButton.click(function(){
            parentThis.showTAFPopup();
            return false;
        });
    },
    
    showTAFPopup: function(){
        var parentThis = this;
		var ajaxUrl = "layoutDomain=Starter&ASIN=" + tafData.tafAsin +
            "&TAFAction=display&layoutId=2";
		JQ.ajax({
           type     : "GET",
           url      : tafPageSecureURL,
           data     : ajaxUrl,
           error    : function(html, status) {alert(status);},
           success  : function(html, status) {
                         parentThis.TAFBuildPage(html);
                         parentThis.TAFShowMain();
                         parentThis.dialogEvents();
			 parentThis.TAFSetFocus();
                     },
           timeout  : 100000
        });
        },

    TAFSendEmail: function()
    {
        var parentThis=this;

        var tafSendEmailUrl="layoutDomain=Starter&ASIN=" + tafData.tafAsin +
			"&TAFAction=send&layoutId=2&TAFTo="+JQ('#TAFTo').attr('value');
        if(JQ('#TAFCCMe').attr('checked')==true)
            tafSendEmailUrl+="&TAFCCMe=true";
        JQ.ajax({
           type     : "GET",
           url      : tafPageSecureURL,
           data     : tafSendEmailUrl,
           error    : function(html, status) {alert(status);},
           success  : function(html, status) {
                         parentThis.TAFBuildPage(html);
                         parentThis.TAFShowMain();
                         parentThis.dialogEvents();
			parentThis.TAFSetFocus();
                     },
           timeout  : 100000
        });
    },
    
    TAFBuildPage: function(nodeName){
        this.tafWindow.empty();
        var RTAFMain = JQ('<div></div>');
        RTAFMain.html(nodeName);
        this.tafWindow.append(RTAFMain);
    },
    
    TAFShowMain: function(){
        this.tafWindow.show();
        this.tafFrame.show();
        var parentThis=this;
        this.tafFrame.fadeTo("slow",0.8,function(){
            parentThis.tafWindow.fadeTo("slow",1);
        });
    },
    
    closeTAFPopUp: function(){
        var parentThis=this;
        this.tafFrame.fadeTo("slow",0,function(){
            parentThis.tafFrame.hide();
        });
        this.tafWindow.fadeTo("slow",0,function(){
            parentThis.tafWindow.hide();
        });
    },
    
    getWindowHeight:function()
    {
        var winH = window.innerHeight;
        if(winH<document.body.clientHeight)
            winH = document.body.clientHeight;
        if(winH<document.documentElement.clientHeight)
            winH = document.documentElement.clientHeight;
        return winH;
    }
};
