/*
 * home.js
 * Controller for home page
 * @author Dan Neame <dneame@britishmuseum.co.uk>
 */

/*
 * Controller Code
 */
var timer_c = parseInt( ( new Date ).getTime() );

$( document ).ready( function(){

var timer_a = parseInt( ( new Date ).getTime() );

	// suppress enter press
	document.onkeypress = BMUI.suppress_enter;
	
	// if on christian art page then inject ad now
//	if( BMUI.Page.urlParts[ 3 ] && BMUI.Page.urlParts[ 4 ]  && BMUI.Page.urlParts[ 3 ] == 'invt' && BMUI.Page.urlParts[ 4 ].indexOf( 'cmc50536' ) > -1 )
//		$( '#product-image-container' ).append( '<img width="300" height="167" src="http://www.britishmuseumshoponline.org/content/ebiz/britishmuseumonlineshop/resources/img/Christ-Ad.gif" style="position:absolute;top:319px"/>' );

	// what do you mean you never played Contra?
	var kkeys = [], konami = "38,38,40,40,37,39,37,39,66,65"; 
	$( document ).keydown( function( e ){ 
		kkeys.push( e.keyCode );
		if ( kkeys.toString().indexOf( konami ) >= 0 ){ 
			$( document ).unbind( 'keydown' , arguments.callee );
			if( BMUI.Page.urlParts[ 4 ] && BMUI.Page.urlParts[ 4 ] == "christmas" ){
				$( 'body' ).append( '<embed style="position:absolute;left:-1000px" src="/content/ebiz/britishmuseumonlineshop/resources/midi/snow.mid" autostart="true"></embed>' );
			}else if( !!document.getElementById( 'jamesallen' )  ){

//				var polaroid = BMUI.PolaroidPicture( document.getElementById( 'jamesallen' ) , document.getElementById( 'wrapper' ) );
//				polaroid.shake();

			}
			//document.body.contentEditable = 'true';
			//document.designMode = 'on';    
		}
	});

	/* listing nav */
	if( $( '#listing-menu' ).size() > 0 ) BMUI.ProductListing.init_categorynav();

	// checkout link adjustment - on click change layout and POST to iframe
	if( $( '#orderconfirmation' ).size() > 0 ) BMUI.Basket.applyCheckoutPopups();

	/* category search */
	BMUI.CategorySplash.init_search();
	BMUI.SearchSuggest.init();
	
	// init basket dropdowns
	BMUI.Basket.init();
	
	// init welcome message
	if( $( '#welcome-message' ).size() > 0 ){
		// if no welcome message cookie, set cookie (so we dont see it again), fade out the welcome message after 6 seconds
		if( $.cookie( 'welcome_message' ) == null ){
			$( '#welcome-message' ).css( { display: 'block' } );
			$.cookie( 'welcome_message' , 'true' , { expires: 1 , path: '/' } );
			setTimeout( "$( '#welcome-message' ).fadeOut( 2000 )" , 6000 );
		}
	}

	// init slideshows
	//BMUI.Slideshow.init();
	BMUI.CategorySplash.init_slideshow();

	if( $( '#home-splash-loadbar' ).size() > 0 ) BMUI.HomeSlide.init();

	// clear after
	BMUI.Page.clear_after( '.formbutton' );

	/* product listing */
	if( $( '.product-listing' ).size() > 0 )
		BMUI.ProductListing.init();

	/* exhibition tab things */
	BMUI.CategorySplash.init_exhibition_splash();

	/* home splash / exh splash / product listing image credits */
	BMUI.ProductListing.init_imagecredits();

	/* my account panel height matching */
	if( $( '#myaccount-membership' ).size() > 0 ){ BMUI.matchHeight( '#myaccount-orderhistory' , '#myaccount-membership' ); }
	if( $( '#myaccount-account-details' ).size() > 0 ){ BMUI.matchHeight( '#myaccount-delivery-details' , '#myaccount-account-details' ); }

	/* pagination feedback */
	BMUI.ProductListing.pagination_feedback();

	/* My account XHR */
	BMUI.UserAccount.init_myaccount();

	/* Inline popups */
	$( '.inline_popup' ).click( function(){
		// if checkout footer links, open near footer
		if( ( $( this ).parent().parent().parent().hasClass( 'checkout-additional-text' ) ) || ( $( this ).parent().parent().parent().attr( 'id' ) == 'additionalservices' ) ) BMUI.InlinePopup.move_to( { top: 590 } );
		var w = 515;
		var h = 550;
		var pY = 100;
		if( $( this ).hasClass( 'dynamic_y' ) ){
			// y position should be scrollTop + 20px
			pY = $( window ).scrollTop() + 20;
			// if y plus height is over document height, set to document height minus height minus 20
			if( ( pY + h ) > $( document ).height() ) pY = $( document ).height() - h - 20;
		}
		// if restricted art print, and usggref matches allowed group names, correct link to art print and change class name
		if( $( this ).hasClass( 'artprintrestricted' ) ){
			var groupname = $( '#usgrref' ).html();
			if( groupname.match( BMUI.bm_member_regexp ) != null && groupname.match( BMUI.bm_member_negative ) == null ){
				$( this ).addClass( 'artprintlaunch' );
				$( this ).removeClass( 'artprintrestricted' );
				$( this ).attr( 'href' , $( this ).attr( 'href' ).replace( /temp=artprintrestricted$/ig , 'temp=artprintpopup' ) );
			}else{
				// resize popup - the "you are not a member" message is very short
				h = 160;
				pY = 380;
			}
		}
		// if has class 'artprintlaunch' then set w to 960 and h to 680
		if( $( this ).hasClass( 'artprintlaunch' ) ){
			w = 960;
			h = 700;
			pY = 10;
			BMUI.trackClick( '/sales/art_print/' );
		}
		if( $( this ).hasClass( 'artprinthelp' ) ){
			w = 578;
			h = 358;
			BMUI.trackClick( '/sales/art_print_help/' );
		}
		// if there's already an iframe popup on the page then we just need to correct the URL and show it
		if( $( '#BMUI_popup-iframe' ).size() > 0 ){
			BMUI.InlinePopup.resize_to( w , h );
			//BMUI.InlinePopup.move_to( { top: pY } );
			BMUI.InlinePopup.set_iframe_url( $( this ).attr( 'href' ) );
			BMUI.InlinePopup.show();
		}else{
			// otherwise we need to create a new one
			BMUI.InlinePopup.create_popup( { width: w , height: h , slideopen: true , url: $( this ).attr( 'href' ) , show: true , scrollbars: true , scrollto: false , panelY: pY } );
		}
		// dont bubble
		return false;
	} );

	/* ticket product page click handler */
	if( $( '#ticket-launch' ).size() > 0 || $( '#ticket-listing' ).size() > 0 ) BMUI.TicketInterface.applyProductPopup();

	/* address book */
	if( $( '#myaccount-address-list' ).size() > 0 ) BMUI.UserAccount.init_addressbook();

	/* email signup */
	BMUI.CategorySplash.init_emailsignup();

	/* basket page upsells - off for now
	if( $( '#basket' ).size() > 0 ){
		BMUI.Basket.injectUpsell();
	} */

	/* valentines date picker */
	$( '.oixtpickupdate > input' ).attr( 'readonly' , 'yes' );
	$( '.oixtpickupdate > input' ).focus( function( e ){
		e.stopPropagation();
		$( '#valentines-datepicker' ).remove();
		$( '#content' ).append( '<div id="valentines-datepicker" style="display:none"></div>' );
		// populate datepicker
		$( '#valentines-datepicker' ).append( '<h3>February 2010</h3><ul><li class="datepicker-header">Mon</li><li class="datepicker-header">Tue</li><li class="datepicker-header">Wed</li><li class="datepicker-header">Thu</li><li class="datepicker-header">Fri</li><li class="datepicker-header">Sat</li><li class="datepicker-header">Sun</li></ul>' );
		for( i=1;i<29;i++ ){
			$( '#valentines-datepicker > ul ' ).append( '<li id="' + i + '-02-2010" class="no-slots"><a href="#">' + i + '</a></li>' );
			if( 10 < i && i < 20 ) $( '#valentines-datepicker > ul > li:last' ).attr( 'class' , 'many-slots' );
			if( i == 14 ) $( '#valentines-datepicker > ul > li:last' ).addClass( 'valentines-day' );
		}
		// click handler
		var inputfield = this; // closure
		// calendar click handler
		$( '#valentines-datepicker > ul > li > a' ).click( function(){
			if( $( this ).parent().hasClass( 'many-slots' ) ){
				$( inputfield ).val( $( this ).parent().attr( 'id' ) );
				$( '#valentines-datepicker' ).hide();
			}
			return false;
		} );
		// fade in
		$( '#valentines-datepicker' ).css( { top: $( this ).position()[ 'top' ] - 297 } ).fadeIn();
	} );
	$( '.oixtpickupdate > input' ).blur( function( e ){
		$( '#valentines-datepicker' ).fadeOut();
		// check the date supplied is one of the allowed dates 
	} );

	/* basket and checkout */
	$( '.basket-grid-action-remove' ).click( function(){
		$( this ).parent().prev().find( 'input' ).val( '0' );
		Step2( document.wizform , 'shopcart' , 'process' , '' , 'updated' , '_self' , '' , '' , '' );
		return false;
	} );

	/* match checkout confirm box sizes */
	if( $( '#orderconfirmation' ).size() > 0 ){
		BMUI.matchHeighest( '#ordersummary' , '#additionalservices' );
		BMUI.matchHeighest( '#paymentdetails' , '#contactdetails' );
		$( '.orderitems' ).each( function(){
			if( $( this ).prev().hasClass( 'deliveryaddress' ) ){
				BMUI.matchHeighest( $( this ) , $( this ).prev() );
			}
		} );
	}
	/* ditto register pages and first 2 panels */
	if( $( '#myaccount-register' ).size() > 0 ){ BMUI.matchHeighest( '#myaccount-register div.myaccount-panel:eq(0)' , '#myaccount-register div.myaccount-panel:eq(1)' ); }
	if( $( '#login-or-register' ).size() > 0 ){ BMUI.matchHeighest( '#login-or-register div.myaccount-panel:eq(0)' , '#login-or-register div.myaccount-panel:eq(1)' ); }

	// hide breadcrumb search and signup on search page
	if( $( '.listing-search' ).size() > 0 ){
		$( '#breadcrumb-signup , .category-splash-search' ).hide();
	}
	
	// culture product click bug
	if( $( '#culture_shop_prodlist-product-listing' ).size() > 0 ){
		$( '#culture_shop_prodlist-product-listing .product-listing-rollover , #culture_shop_prodlist-product-listing h4 , #culture_shop_prodlist-product-listing p' ).click( function(){
			$( this ).parent().find( 'a:first' ).click();
		});
		$( '#culture_shop_prodlist-product-listing img' ).click( function(){
			self.location = $( this ).attr( 'rel' );
		} );
	}
	
	// slide credits that span 2 lines
	$( '.slide-credit' ).each( function(){
		if( parseInt( $( this ).find( 'span' ).height() ) > 20 ){
			$( this ).find( 'span' ).css( { top: ( 12 - parseInt( $( this ).find( 'span' ).height() ) )  } ); /* breaks MVC pattern */
		}
	});
	
	// hover on free delivery
	$( '#breadcrumb-freedelivery-hit' ).hover( function(){ $( '#breadcrumb-freedelivery-hover' ).fadeIn( BMUI.config[ 'animation_speed' ] ); } , function(){ $( '#breadcrumb-freedelivery-hover' ).fadeOut( BMUI.config[ 'animation_speed' ] ); } );
	// twitter
	$( '#breadcrumb-twitter-hit' ).hover( function(){ $( '#breadcrumb-twitter-hover' ).fadeIn( BMUI.config[ 'animation_speed' ] ); } , function(){ $( '#breadcrumb-twitter-hover' ).fadeOut( BMUI.config[ 'animation_speed' ] ); } );
	// and facebook
	$( '#breadcrumb-facebook-hit' ).hover( function(){ $( '#breadcrumb-facebook-hover' ).fadeIn( BMUI.config[ 'animation_speed' ] ); } , function(){ $( '#breadcrumb-facebook-hover' ).fadeOut( BMUI.config[ 'animation_speed' ] ); } );

	/* RVI */
	// save product view
	/* init product */
	if( $( '#product-media' ).size() > 0 || $( '#oixform' ).size() > 0 ){
		BMUI.Product.init();
	}
	if( $( '#product-media' ).size() > 0 ){
		BMUI.Product.init_media();
		BMUI.RVI.viewItem( BMUI.Page.urlParts[ 4 ].split( '?' )[ 0 ] );
		BMUI.RVI.saveItems();
	}
	// membership product check
	if( $( '#oixform' ).size() > 0 ){
		BMUI.Product.check_membership_getvars();
	}
	// membership basket message
	// only show if extra card (NOT GUEST PASS!) is in basket
	if( $( '#membership-addcard-msg' ).size() > 0 ){
		$( '.basket-grid-sku' ).each( function(){ if( $( this ).html() == 'fmemexcard' ) $( '#membership-addcard-msg' ).show(); } );
	}

	// inject products
	if( $( '#rvi-container' ).size() > 0 ){
		BMUI.RVI.injectItems();
	}
	
// timing
var timer_b = parseInt( ( new Date ).getTime() );
if( BMUI.Page.hasHashTag( 'debugjs' ) ) alert( 'DOM took ' + (  timer_c - timer_a ) / 1000 + 's to load' );
if( BMUI.Page.hasHashTag( 'debugjs' ) ) alert( 'JS took ' + (  timer_b - timer_a ) / 1000 + 's to execute' );
	
} );

// legacy venda
function setequalheight(a,b,c){};
function openNav(a,b){};
function appendText(a,b){};
Venda = {};
Venda.Ebiz = {};
Venda.Ebiz.splitString = function(a,b,c){};
CookieJar = function(a){this.get=function(a){return null;};};
var countTicket = 0;
