//	jQuery Largebox plugin
//	.based on jQuery lightbox plugin by Benjamin "balupton" Lupton {http://www.balupton.com}
//	.modified and tailored by Story Worldwide

$jq(document).ready(function() {

    $jq(".quickview_button").largebox({
        name: 'quickview',
        width: 765,
        top: 75,
        type: 'div',
        height: 485,
        stripAt: 'no_strip',
        callback: 'handle_tabs',
        dynamicAction: true
    });

    $jq("#the-proposal_terms").largebox({
        name: 'theproposalTermsLargebox',
        width: 450,
        top: 155,
        type: 'html',
        height: 450,
        action: '/popups/proposal_popup.tmpl'
    });
    
    $jq('#pop_email_signup_successbox').largebox({
        name: 'email_signup_successLargebox',
        width: 419,
        top: 155,
        type: 'html',
        height: 195,
        action: '/templates/includes/dhtml_popups/cs_popup.tmpl?page=email_signup_successbox'
    });  
    
    $jq('#pop_free_prize_draw_successbox').largebox({
        name: 'free_prize_draw_successLargebox',
        width: 419,
        top: 155,
        type: 'html',
        height: 195,
        action: '/templates/includes/dhtml_popups/cs_popup.tmpl?page=free_prize_draw_successbox'
    });

    $jq("#pop_free_prize_draw_terms").largebox({
        name: 'free_prize_drawLargebox',
        width: 450,
        top: 155,
        type: 'html',
        height: 450,
        action: '/templates/includes/dhtml_popups/cs_popup.tmpl?page=free_prize_draw_terms'
    });    

    $jq('#pop_fb_competition_successbox').largebox({
        name: 'fb_successLargebox',
        width: 419,
        top: 155,
        type: 'html',
        height: 195,
        action: '/templates/includes/dhtml_popups/cs_popup.tmpl?page=fb_competition_successbox'
    });

    $jq("#pop_fb_competition_terms").largebox({
        name: 'fb_competitionLargebox',
        width: 450,
        top: 155,
        type: 'html',
        height: 450,
        action: '/templates/includes/dhtml_popups/cs_popup.tmpl?page=fb_competition_terms'
    });
    
    $jq('#pop_bg_competition_successbox').largebox({
        name: 'bg_successLargebox',
        width: 419,
        top: 155,
        type: 'html',
        height: 195,
        action: '/templates/includes/dhtml_popups/cs_popup.tmpl?page=bg_competition_successbox'
    });

    $jq("#pop_bg_competition_terms").largebox({
        name: 'bg_competitionLargebox',
        width: 450,
        top: 155,
        type: 'html',
        height: 450,
        action: '/templates/includes/dhtml_popups/cs_popup.tmpl?page=bg_competition_terms'
    });    


    $jq('#pop_mc_competition_successbox').largebox({
        name: 'mc_successLargebox',
        width: 419,
        top: 155,
        type: 'html',
        height: 195,
        action: '/templates/includes/dhtml_popups/cs_popup.tmpl?page=mc_competition_successbox'
    });

    $jq("#pop_mc_competition_terms, #pop_mc_competition_terms1").largebox({
        name: 'mc_competitionLargebox',
        width: 450,
        top: 155,
        type: 'html',
        height: 450,
        action: '/templates/includes/dhtml_popups/cs_popup.tmpl?page=mc_competition_terms'
    });



	
    $jq('#pop_anr_prize_draw_successbox').largebox({
        name: 'anr_prize_draw_successLargebox',
        width: 450,
        top: 155,
        type: 'html',
        height: 100,
        action: '/templates/includes/dhtml_popups/cs_popup.tmpl?page=anr_prize_draw_successbox'
    });		
	
    $jq("#pop_anr_prize_draw_terms").largebox({
        name: 'anr_prize_drawLargebox',
        width: 450,
        top: 155,
        type: 'html',
        height: 450,
        action: '/templates/includes/dhtml_popups/cs_popup.tmpl?page=daywear_comp_draw_terms'
    });	
    
    $jq('#pop_daywear_comp_successbox').largebox({
        name: 'daywear_comp_successLargebox',
        width: 419,
        top: 155,
        type: 'html',
        height: 200,
        action: '/templates/includes/dhtml_popups/cs_popup.tmpl?page=daywear_comp_successbox'
    });		
	
    $jq("#pop_daywear_comp_terms").largebox({
        name: 'daywear_compLargebox',
        width: 450,
        top: 155,
        type: 'html',
        height: 450,
        action: '/templates/includes/dhtml_popups/cs_popup.tmpl?page=daywear_comp_terms'
    });	

    $jq("#pop_bronzegoddess_terms").largebox({
        name: 'bronzegoddessLargebox',
        width: 450,
        top: 155,
        type: 'html',
        height: 450,
        action: '/templates/includes/dhtml_popups/cs_popup.tmpl?page=bronzegoddess_terms'
    });

    $jq("#the-proposal_popup").largebox({
        name: 'theProposalPopupLargebox',
        width: 755,
        top: 85,
        type: 'html',
        height: 755,
        dynamicAction: true
    });

    $jq(".dynamic_popup").largebox({
        name: 'dynamicPopupLargebox',
        width: 450,
        top: 155,
        type: 'html',
        height: 480,
        dynamicAction: true
    });
    
    $jq(".coming_soon_btn").largebox({
        name: 'comingSoonLargebox',
        width: 370,
        top: 155,
        type: 'html',
        height: 230,
        dynamicAction: true
    });
    
    $jq(".gift_time_link").largebox({
        name: 'giftTimeLargebox',
        width: 370,
        top: 155,
        type: 'html',
        height: 210,
        dynamicAction: true
    });

    $jq(".skuInventoryStatus").largebox({
        name: 'backInStockLargebox',
        width: 370,
        top: 155,
        type: 'html',
        height: 210,
        dynamicAction: true
    });
    
    $jq(".mppInventoryStatus").largebox({
        name: 'backInStockLargeboxMpp',
        width: 370,
        top: 155,
        type: 'html',
        height: 210,
        dynamicAction: true
    });
    
    $jq("#popup_info").largebox({
        name: 'infoLargebox',
        width: 450,
        top: 155,
        type: 'html',
        height: 450,
        action: '/templates/includes/dhtml_popups/cs_popup.tmpl?page=additional_shipping'
    });

    $jq("#popup_delivery").largebox({
        name: 'deliveryLargebox',
        width: 450,
        top: 155,
        type: 'html',
        height: 450,
        action: '/templates/includes/dhtml_popups/cs_popup.tmpl?page=delivery_methods'
    });

    $jq("#popup_terms").largebox({
        name: 'termsLargebox',
        width: 450,
        top: 155,
        type: 'html',
        height: 450,
        action: '/templates/includes/dhtml_popups/cs_popup.tmpl?page=terms'
    });

    $jq("#popup_returns").largebox({
        name: 'returnsLargebox',
        width: 450,
        top: 155,
        type: 'html',
        height: 450,
        action: '/templates/includes/dhtml_popups/cs_popup.tmpl?page=returns_policy'
    });

    $jq("#popup_payment").largebox({
        name: 'paymentLargebox', 
        width: 600,
        top: 155,
        type: 'html',
        height: 250,
        action: '/templates/includes/dhtml_popups/cs_popup.tmpl?page=payment_methods'
    });
    
    $jq("#homepage_popup").largebox({
        name: 'homepageLargebox',
        width: 673,
        top: 155,
        type: 'html',
        height: 338,
        action: '/templates/includes/dhtml_popups/homepage_popup.tmpl'
    });
    
    $jq("#ffinder_popup").largebox({
        name: 'ffinderLargebox',
        width: 675,
        top: 225,
        type: 'html',
        height: 354,
        action: '/templates/includes/dhtml_popups/ffinder_popup.tmpl'
    });    
});

(function($jq){
	
	var $largeboxes = new Array();
	
	$jq.LargeboxClass = function () {
		this.construct();
	};
	
	$jq.fn.largebox =  function( params ) {
		var defaults = {
		    name:       'default',
			top:		70, 	// distance from top of page
			width: 		350,	// width of box															CHANGED
			height: 	0,	// height of box (default of 0 will not set a fixed height)
			type:		'html', // default type
			opacity: 	0.9,	// opacity value
			stripAt: 	'body', // when parsing a html document via AJAX this is the tag that wraps the content to display in the box 
			events: 	true,
			border:		5,
			method:   	'GET',
			formName:	'',
			callback:   '',
			action:		'',
			dynamicAction: false
		};
		params = $jq.extend(defaults,params);
		$jq.Largebox = $largeboxes[params.name] || new $jq.LargeboxClass();
		$jq.Largebox.populateParams(params);
        $largeboxes[params.name] = $jq.Largebox;
		if (params.events) {
            $jq(this).live("click", function() {
                $jq('#samples_button').hide();
                $jq('#samples_loader').show();
                $jq.Largebox = $largeboxes[params.name];
                $jq.Largebox.callingElementId = $jq(this).attr('id');
                if ($jq.Largebox.dynamicAction) {
                    if ($jq.Largebox.type == 'html') {
                        $jq.Largebox.action = $jq('#' + $jq(this).attr('id') + '_largebox_href').text();
                    } else {
                        $jq.Largebox.action = $jq('#' + $jq(this).attr('id') + '_largebox_' + $jq.Largebox.type).text();
                    }
                }
                if (!$jq.Largebox.start()) return true;    // supply the variable hrefdata to the start() function instead

                //    Largebox will load the href of it's assigned tag into itself
                return false;
            })
        }

	};
	
	$jq.extend($jq.LargeboxClass.prototype,{
		top:		null,
		width: 		null,
		height: 	null,
		padding: 	null,
		type: 		null,
		opacity: 	null,
		stripAt: 	null,
		border:	 	null,
		method:		null,
		formName:	null,
		action: 	null,
		speed: 		400,
		dynamicAction: null,
		callback:   null,
		callingElementId: null,
		construct: function () {
			return true;
		},
		tree: null,
		populateParams: function (params) {
			$jq.Largebox.top = 			params.top;
			$jq.Largebox.width = 		params.width;
			$jq.Largebox.height = 		params.height;
			$jq.Largebox.opacity = 		params.opacity;
			$jq.Largebox.type = 		params.type;
			$jq.Largebox.stripAt =		params.stripAt;
			$jq.Largebox.border =		params.border;
			$jq.Largebox.method =		params.method;
			$jq.Largebox.formName =		params.formName;
			$jq.Largebox.callback =     params.callback;
			$jq.Largebox.action =		params.action;
			$jq.Largebox.dynamicAction = params.dynamicAction;
		},
		start: function () {
			var tree = null;
			var boxy = $jq.Largebox
			
			switch($jq.Largebox.type) {
				case 'html':
    				$jq.ajax({
    					type: $jq.Largebox.method,
    					url: $jq.Largebox.action,
    					cache: false,
    					success: function (data) {
    						//	what to do with data from the ajax call
    						tree = $jq.Largebox.parseDocument(data);
    						$jq.Largebox.appendDOM(tree);
    						$jq('#largebox').bgiframe();
                			$jq('#largebox-overlay').height($jq(document).height());
                			// scroll to top of page
    						if ($jq.Largebox.callback) {
    						    var callbackRan = false;
                                $jq('html, body').animate({scrollTop:0}, 'slow', 'linear', function() {
                                    if (!callbackRan) {
                                        eval($jq.Largebox.callback + "('" + $jq.Largebox.callingElementId + "')");
                                        callbackRan = true;
                                    }
                                });
                            } else {
                                $jq('html, body').animate({scrollTop:0}, 'slow');
                            }
    						$jq('#samples_loader').hide();
    					},
    					error: function(res) {
    						//console.log(res);  
    					}
    				})
    				break;
    		    case 'div':
    		        data = $jq('#' + $jq.Largebox.action).html();
    		        tree = $jq.Largebox.parseDocument(data);
					$jq.Largebox.appendDOM(tree);
					$jq('#largebox').bgiframe();
        			$jq('#largebox-overlay').height($jq(document).height());
					// scroll to top of page
                    if ($jq.Largebox.callback) {
    				    var callbackRan = false;
                        $jq('html, body').animate({scrollTop:0}, 'slow', 'linear', function() {
                            if (!callbackRan) {
                                eval($jq.Largebox.callback + "('" + $jq.Largebox.callingElementId + "')");
                                callbackRan = true;
                            }
                        });
                    } else {
                        $jq('html, body').animate({scrollTop:0}, 'slow');
                    }
					$jq('#samples_loader').hide();
    		        break;
			}
			return true;
		},
		parseDocument: function (data) {
			//	finds where to fetch html from, omitting the head and html tags
			var start, end;
			if ($jq.Largebox.stripAt == 'no_strip') {
			    return data;
			} else if ($jq.Largebox.stripAt != 'body') {
				return $jq(data).find($jq.Largebox.stripAt).children();
			} else {
				start = data.indexOf('<body>')+6;
				end = data.indexOf('</body>')
				if(start>-1 && (end>-1 && end>start)){
					return data.substring(start,end);
				} else return 'Document is not valid XHTML';
			}
		},
		clearDOM: function(tree){
			$jq('#largebox-overlay').remove();
			$jq('#largebox').remove();
			var overlay = $jq('<div id="largebox-overlay"></div>');
			var thebox = $jq('<div id="largebox"></div>');
			var close_button = $jq('<div id="largebox-close"></div>');
			$jq(thebox).css('width',$jq.Largebox.width+'px').css('top',$jq.Largebox.top+'px').hide();			//	removed the fixed height by default
			//only fix the height if a height was supplied
			if ($jq.Largebox.height != 0) {
			    $jq(thebox).css('height',$jq.Largebox.height+'px');
			};
			$jq('body').append($jq(overlay));
			$jq('body').append($jq(thebox));
			//	click anywhere on the overlay to close the box
			$jq(overlay).unbind().click(function() {
			    //removed at request of brand.
				//$jq.Largebox.finish();
				return false;	
			});
			//	close button
			$jq(thebox).append($jq(close_button));
			$jq(close_button).click(function (){
				$jq.Largebox.finish();
			});		
			//$jq.Largebox.resizeOverlay();
		    $jq.Largebox.repositionBox();
			$jq(overlay).css('opacity',$jq.Largebox.opacity).fadeIn(400, function(){
				if(tree)
				{
					$jq(thebox).append($jq(tree)).fadeIn(300);
				}
				else
				{
				  var mar_tb =  ($jq.Largebox.height - 32) / 2;
				  var mar_rl =  ($jq.Largebox.width - 32) / 2;
				  var margin = 'margin:' + mar_tb + 'px ' + mar_rl + 'px ' + mar_tb + 'px ' + mar_rl + 'px;';
					$jq(thebox).append('<div style="' + margin + ' width: 32px; height:32px;"><img src="/images/ajax-loader.gif" height="32" width="32" /></div>').fadeIn(300);
				}
			});
			//	if there is an element with the id close_box, assign a closing function to it
			if ($jq('#close_box')!=null) {
				$jq('#close_box').unbind().click(function() {
					$jq.Largebox.finish();
					return false;	
				});
			}
		},
		appendDOM: function(tree) {
			//console.log(tree);
			//																										ADDED 090908 WB
			$jq.Largebox.clearDOM(tree);
			//																										ADDED 310708						
			

			//																										END ADDED
			$jq(window).resize(function () {
			     //$jq.Largebox.resizeOverlay();
			     $jq.Largebox.repositionBox();
			});
			//$jq(window).scroll(function () { $jq.Largebox.repositionBox(); });
		},
		resizeOverlay: function ( ) {
			$jq('#largebox-overlay').css({
				width:		$jq(window).outerwidth,
				height:		$jq(window).outerheight
			});
			//	ie6...
			//if (document.documentElement) $jq('#largebox-overlay').css('height','2000px');
			
			var htmlHeight   = $jq("html").height();
			var windowHeight = $jq(window).height();
					
			var sHeight = (htmlHeight > windowHeight) ? htmlHeight : windowHeight;
			if (document.documentElement) $jq('#largebox-overlay').css('height',sHeight + 'px');
		},
		repositionBox: function ( )		{
			// Get page scroll
			//var pageScroll = this.getPageScroll();
			
			//var nTop = pageScroll.yScroll + $jq.Largebox.top;
			nTop = $jq.Largebox.top;
			var nLeft = Math.round(($jq(document).width()/2))-Math.round($jq.Largebox.width/2);	
		
			var outercss = {
				left: 0,
				top: 0
			};
			
			var innercss = {
				left: nLeft,
				top: nTop
			};
			
			$jq('#largebox').css(innercss);
			$jq('#largebox-overlay').css(outercss);
		},
		getPageScroll: function ( ) {
			var xScroll, yScroll;
			if (self.pageYOffset)
			{	// Some browser
				yScroll = self.pageYOffset;
				xScroll = self.pageXOffset;
			} else if (document.documentElement && document.documentElement.scrollTop)
			{	// Explorer 6 Strict
				yScroll = document.documentElement.scrollTop;
				xScroll = document.documentElement.scrollLeft;
			} else if (document.body)
			{	// All other browsers
				yScroll = document.body.scrollTop;
				xScroll = document.body.scrollLeft;	
			}
			var arrayPageScroll = {'xScroll':xScroll,'yScroll':yScroll};
			return arrayPageScroll;
		},
		finish: function ( )
		{
			$jq('#largebox').hide();
			$jq('#largebox-overlay').remove();
			$jq('#largebox').remove();
			$jq('#samples_button').show();
		}
	})
	
	$jq(function() {
		$jq.Largebox = $jq.Largebox || new $jq.LargeboxClass();
	});

})(jQuery);

