$(document).ready(function() {
	
	$('#sign-in, a[title="Sign In"]').click(function() {
		var mylocation = document.location.href.replace("http://www.weatherbysshop.co.uk/", "");
		if(mylocation == 'not-signed-in'){
			mylocation = 'user';
		}
		create_popup('/user?content_only&destination='+mylocation);
		return false;
	});
	
	$('#register').click(function() {
		create_popup('/user/register?content_only&destination=home');
		return false;
	});
	
	$(document).keyup(function(e) {
		//if (e.keyCode == 27) { $('.close').click(); }   // esc
	});
	
    $('input.workflow-button-discard-create-new').click( function(event) {
      event.preventDefault();
      window.location = '/basket/checkout';
    });
    
    if(document.location.href.indexOf("/basket/checkout") != -1) {
    	//countDown();
    }
    
    // Fix to make menu all align correctly in IE7
    if ($.browser.msie && $.browser.version.substr(0,1)==7) {
    	$('a[href$="betting-services"]').next().css('margin-left','-108px');
    }
    
    // Fix to make drop down menu appear infront of page elements in IE7
	$(".mainmenubar").hover(
		function () {
			if ($.browser.msie && $.browser.version.substr(0,1)==7) $('.sliderwrapper, .noboerder, .intbody ul.tabsm, #block-views-recently_viewed-block_1').css('z-index', '-1');
		},
		function () {
			if ($.browser.msie && $.browser.version.substr(0,1)==7) $('.sliderwrapper, .noboerder, .intbody ul.tabs, #block-views-recently_viewed-block_1').css('z-index', '0');
		}
	);
    
});

function make_change() {
    //console.log('change');
	var contents = $('#line-items-div').html();
    contents = contents.replace('Order total preview:','<div class="preview">Order total preview:</div>');
    $('#line-items-div').html(contents);
    $('a[href="/ajax_register/login?destination=basket/checkout"]').attr('title','Sign In').attr('href','#');
	$('#sign-in, a[title="Sign In"]').click(function() {
		var mylocation = document.location.href.replace("http://weatherbysshop.co.uk/", "");
		create_popup('/user?content_only&destination='+mylocation);
		return false;
	});
}
function checkForDiv() {
	//console.log('check');
	if($('#line-items-div').length==1 && $('#line-items-div').html().indexOf("Order total preview:") != -1) {
		make_change();
	} else {
		countDown();
	}
}
function countDown() {
	var t=setTimeout("checkForDiv()",200);
}
function close_popup() {
	$('#boxes').fadeOut('fast',function(){
		$('#boxes').remove();
	});
	return false;
}
function create_popup(href) {
	//console.log('create popo');
	var html = '<div id="boxes">\
				    <div id="dialog" class="window">\
				    	<a href="#" class="close">Close</a>\
				    	<iframe scrolling="no" frameBorder="0" id="iframe-content" src="'+href+'"></iframe>\
				    </div>\
				    <div id="mask"></div>\
				</div>';
	
	$('body').prepend(html);
	
	//Get the screen height and width
    var maskHeight = $(document).height();
    var maskWidth = $(window).width();
 
    //Set height and width to mask to fill up the whole screen
    $('#mask').css({'width':maskWidth,'height':maskHeight});
     
    //transition effect    
    $('#mask').fadeIn(1000);   
    //$('#mask').fadeTo("slow",0.8); 
 
    //Get the window height and width
    var winH = $(window).height();
    var winW = $(window).width();
           
    //Set the popup window to center
    $('#dialog').css('top',  winH/2-$('#dialog').height()/2);
    $('#dialog').css('left', winW/2-$('#dialog').width()/2);
 
    //transition effect
    $('#dialog').fadeIn(2000); 
    
    $('#mask, .close').click(function() {
		$('#boxes').fadeOut('fast',function(){
			$('#boxes').remove();
		});
		return false;
    });
}
