//	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("#the-proposal_terms").largebox({
        name: 'proposaltermsLargebox',
        width: 450,
        top: 155,
        type: 'html',
        height: 450,
        action: '/popups/proposal_popup.tmpl'
    });

    $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?href=/service_client/shipping_popup.tmpl'
    });

    $jq("#popup_delivery2").largebox({
        name: 'deliveryLargebox2',
        width: 450,
        top: 155,
        type: 'html',
        height: 450,
        action: '/templates/includes/dhtml_popups/cs_popup.tmpl?href=/service_client/shipping_popup.tmpl'
    });

    $jq("#popup_terms").largebox({
        name: 'termsLargeBox',
        width: 655,
        top: 50,
        type: 'html',
        height: 600,
        action: '/templates/includes/dhtml_popups/cs_popup.tmpl?href=/service_client/terms_popup.tmpl'
    });

    $jq("#popup_privacy").largebox({
        name: 'privacyLargebox',
        width: 655,
        top: 50,
        type: 'html',
        height: 600,
        left: 450,
        action: '/templates/includes/dhtml_popups/cs_popup.tmpl?href=/service_client/privacy_popup.tmpl'
    });

    $jq("#popup_postcode").largebox({
        name: 'postcodeLargebox',
        width: 800,
        top: 50,
        type: 'html',
        height: 600,
        left: 450,
        action: '/templates/includes/dhtml_popups/cs_popup.tmpl?href=http://www1.auspost.com.au/postcodes/'
    });

    $jq("#popup_security").largebox({
        name: 'securityLargebox',
        width: 450,
        top: 155,
        type: 'html',
        height: 450,
        action: '/templates/includes/dhtml_popups/cs_popup.tmpl?href=/service_client/security_popup.tmpl'
    });

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

    $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("#popup_privacy_small").largebox({
        name: 'privacySmallLargebox',
        width: 660,
        top: 155,
        type: 'html',
        height: 450,
        action: '/templates/includes/dhtml_popups/cs_popup.tmpl?href=/service_client/privacy_popup.tmpl'
    });

    $jq("#popup_terms_small").largebox({
        name: 'termsSmallLargebox',
        width: 660,
        top: 155,
        type: 'html',
        height: 450,
        action: '/templates/includes/dhtml_popups/cs_popup.tmpl?href=/service_client/terms_popup.tmpl'
    });

    $jq(".popup_sp_quickview").largebox({
        name: 'spquickviewLargebox',
        width: 740,
        top: 75,
        type: 'html',
        height: 470,
        dynamicAction: true
    });

});

(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:	'',
			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];
                if ($jq.Largebox.dynamicAction) {
                    $jq.Largebox.action = $jq('#' + $jq(this).attr('id') + '_largebox_href').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,
		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.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
                                         
                                               // For QV CM tags 
                                             if (typeof mpp_tag != "undefined"){
                                                  cm_prodid = data.match("PROD[0-9]+");
                                                 if (typeof cm_prodid != "undefined"){   
                                                     mpp_tag(data.match("PROD[0-9]+"),1,1);
                                                 }
                                             }
						tree = $jq.Largebox.parseDocument(data);
						$jq.Largebox.appendDOM(tree);
						$jq('#largebox').bgiframe();
            			$jq('#largebox-overlay').height($jq(document).height() + 15);
						// scroll to top of page
						$jq('html, body').animate({scrollTop:0}, 'slow');
						$jq('#samples_loader').hide();
					},
					error: function(res) {
						//console.log(res);
					}
				})
				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 != '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">FERMER</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() {
				$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="/common/img/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);

function quickviewFlash(categoryId, productId) {
    //alert(categoryId+' - "'+productId+'"'); return;
    $jq('#' + productId).trigger('click');
}

