jQuery.noConflict();

jQuery(document).ready(function() {
   	init();
});


function resizeWindow(iWidth, iHeight){  
    var resizeRef = document.getElementById('resizeReference');  
    var iOuterWidth = iWidth + 10;  
    var iOuterHeight = iHeight + 29;  
   
    if (resizeRef) {  
        var iPreWidth = resizeRef.offsetWidth;  
        var iPreHeight = resizeRef.offsetHeight;  
        window.resizeTo(iPreWidth,iPreHeight);  
        var iPostWidth = resizeRef.offsetWidth;  
        var iPostHeight = resizeRef.offsetHeight;  
        iOuterWidth = iWidth + (iPreWidth-iPostWidth);  
        iOuterHeight = iHeight + (iPreHeight-iPostHeight);  
    }  
    window.resizeTo(iOuterWidth, iOuterHeight);  
}


function init() {

	/* Detect browser
	------------------------------------- */
	
	if (jQuery(".resize-client").attr("id") == "resize-client") {
		var userAgent = navigator.userAgent.toLowerCase();
		var browserType;
    	jQuery.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase()); 
    	
    	// Is this a version of IE?
    	if(jQuery.browser.msie) {
    	    browserType = "ie";
    	    
    	    // Add the version number
    	    browserType = 'ie' + jQuery.browser.version.substring(0,1);
    	}    
    	
    	// Is this a version of Chrome?
    	if(jQuery.browser.chrome) {
    	
    	    browserType = "chrome";
    	    
    	    //Add the version number
    	    userAgent = userAgent.substring(userAgent.indexOf('chrome/') +7);
    	    userAgent = userAgent.substring(0,1);
    	    browserType = 'chrome';
    	    
    	    // If it is chrome then jQuery thinks it's safari so we have to tell it it isn't
    	    jQuery.browser.safari = false;
    	}
    	
    	// Is this a version of Safari?
    	if(jQuery.browser.safari) {
    	    browserType = 'safari';
    	    
    	    // Add the version number
    	    userAgent = userAgent.substring(userAgent.indexOf('version/') +8);
    	    userAgent = userAgent.substring(0,1);
    	    browserType = 'safari';
    	}
    	
    	// Is this a version of Mozilla?
    	if(jQuery.browser.mozilla) {
    	    
    	    //Is it Firefox?
    	    if(navigator.userAgent.toLowerCase().indexOf('firefox') != -1){
    	        browserType = 'ff';
    	        
    	        // Add the version number
    	        userAgent = userAgent.substring(userAgent.indexOf('firefox/') +8);
    	        userAgent = userAgent.substring(0,1);
    	        browserType = 'ff';
    	    }
    	    // If not then it must be another Mozilla
    	    else {
    	        browserType = 'mozilla';
    	    }
    	}
    	
    	// Is this a version of Opera?
    	if(jQuery.browser.opera){
    	    jQuery('body').addClass('browserOpera');
    	    browserType = 'opera';
    	}    
    }

	/* Resize client
	------------------------------------- */
	
	/* Small */
	jQuery("#client-size-small").click(function() {
	
		// Resize browser
		/*
		if (browserType == "ff") {
			window.resizeTo(607, 600);
		} else if (browserType == "ie6" || browserType == "ie7") {
			window.resizeTo(610, 585);
		} else if (browserType == "chrome") {
			window.resizeTo(610, 580);
		} else {
			window.resizeTo(600, 580);
		}
		*/
		
		resizeWindow(600, 580);
		
		// Flash		
		jQuery("#flash-middle").attr({
			width: 440,
    		height: 330    		
   		});
   		
   		// Set active class
   		jQuery(this).addClass("active");
   		jQuery("#client-size-medium").removeClass("active");
   		jQuery("#client-size-large").removeClass("active");
		
		return false;
	});
	
	/* Medium */
	jQuery("#client-size-medium").click(function() {
	
		// Resize browser
		if (browserType == "ff") {
			window.resizeTo(807, 750);
		} else if (browserType == "ie6" || browserType == "ie7") {
			window.resizeTo(810, 735);
		} else if (browserType == "chrome") {
			window.resizeTo(810, 730);
		} else {
			window.resizeTo(800, 730);
		}
		
		// Flash		
		jQuery("#flash-middle").attr({
			width: 640,
    		height: 480    		
   		});
   		
   		// Set active class
   		jQuery(this).addClass("active");
   		jQuery("#client-size-small").removeClass("active");
   		jQuery("#client-size-large").removeClass("active");
		
		return false;
	});
	
	/* Large */
	jQuery("#client-size-large").click(function() {
	
		// Resize browser
		max_height = screen.availHeight;
		browser_width = Math.floor(max_height * 1.212);
		
		if (browserType == "ie7") {
			window.resizeTo(browser_width-65, max_height);
		} else {
			window.resizeTo(browser_width-40, max_height);
		}
		
		
		// Flash
		if (browserType == "ff") {
			flash_height = Math.floor(max_height-265);
			flash_width = Math.floor(flash_height*1.333);
		} else if (browserType == "ie6") {
			flash_height = Math.floor(max_height-268);
			flash_width = Math.floor(flash_height*1.333);
		} else if (browserType == "ie7") {
			flash_height = Math.floor(max_height-270);
			flash_width = Math.floor(flash_height*1.333);
		} else if (browserType == "chrome") {
			flash_height = Math.floor(max_height-266);
			flash_width = Math.floor(flash_height*1.333);
		} else {
			flash_height = Math.floor(max_height-250);
			flash_width = Math.floor(flash_height*1.333);
		}

//        if(flash_width > 600)flash_width = 600; 
		
		jQuery("#flash-middle").attr({
			width: flash_width,
    		height: flash_height
   		});
   		    
   		// Set active class
   		jQuery(this).addClass("active");
   		jQuery("#client-size-small").removeClass("active");
   		jQuery("#client-size-medium").removeClass("active");
		
		return false;
	});


    /* Menu in client
    ------------------------------------- */

    var t;

    // First
    jQuery("a#games").mouseover(function() {
        jQuery("ul.games-first-level").show();
        clearTimeout(t);
    });

    jQuery("a#games").bind("mouseleave", function() {
        t = setTimeout(function() { jQuery("ul.games-first-level").hide() }, 500);
    });

    // Second
    jQuery(".client-menu ul li").mouseover(function() {
        jQuery("ul.games-second-level", this).show();
        clearTimeout(t);
    });

    jQuery("ul.games-first-level li").bind("mouseleave", function() {
        jQuery("ul.games-second-level", this).hide();
    });

    // Third
    jQuery("ul.games-second-level li").mouseover(function() { 
        jQuery("ul.games-third-level", this).show();
        clearTimeout(t);
    });

    jQuery("ul.games-second-level li").bind("mouseleave", function() {
        jQuery("ul.games-third-level", this).hide();
    });

    // Leave
    jQuery("ul.games-first-level").bind("mouseleave", function() {
        t = setTimeout(function() { jQuery("ul.games-first-level").hide() }, 500);
    });
	
	/* Check for IE6
	------------------------------------- */
	isIE6 = /msie|MSIE 6/.test(navigator.userAgent);


	/* Set background in iframe
	------------------------------------- */
	
	if (jQuery("#wrap").attr("class") == "account-reduced-width") {
		jQuery("body").css("background-image", "none");
		jQuery("body").css("background-color", "transparent");
	}
	
	
	
	

	 
	/* Header login form
	------------------------------------- */
	jQuery(".username").focus(function() {
		var val = jQuery(this).val();
		if(val == "Användarnamn") {
			jQuery(this).val('');
		}
	});
	
	jQuery(".password").focus(function() {
		var val = jQuery(this).val();
		if(val == "Lösenord") {
			jQuery(this).val('');		
		}
	});

	
	
	/* Puff buttons
	------------------------------------- */
	jQuery(".puff").each(function() {
		btn_width = jQuery(this).find('.puff-footer .button').width();
		jQuery(this).find('.puff-footer .button').css('float', 'none').css('width', btn_width+2).css('margin', '10px auto');
		
		header_width = jQuery(this).find('.puff-header h3').width()+90;
		this_margin = (jQuery(this).width()-header_width)/2;
		if(isIE6) {
			this_margin = this_margin+11;
		}
		var o = jQuery(this).find('.puff-header h3');
	    if(!o.hasClass("last"))o.css('margin-left',this_margin+'px')
	})
	
	jQuery(".puff:last-child").addClass('last');
	//jQuery(".sidebar .puff:last-child").removeClass('last');
	jQuery(".sidebar .puff:first-child").addClass('first');
	
	
	/* Payment forms
	------------------------------------- */
	jQuery(".ajax-hidden").css('display','none');
	
	jQuery(".header-row a").click(function() {
		var target = jQuery(this).attr('id');
		var state = jQuery('#form-block-'+target).css('display');
		jQuery(".ajax-hidden").hide('fast');
		if(state != 'block') {
			jQuery('#form-block-'+target).show('fast');
			jQuery('#text-'+target).show('fast');
		}
		return false;
	});
		
}


/* Hide Client
------------------------------------- */

var toggled=false;
var lastDims=[0,0];
var dims=[200,140];
var lastpos=[0,0];
var pos=[0,0];
function resizeOuterTo() {
    if(toggled == true){
    	var ie7 = (document.all && !window.opera && window.XMLHttpRequest) ? true : false;
    	dims=lastDims;
    	pos[0] = lastpos[0];
    	pos[1] = lastpos[1];
    	toggled = false;
    	self.moveTo(pos[0],pos[1]);
    	if(navigator.appName == "Netscape"){
    		top.resizeTo(dims[0],dims[1]);
    	}else if(ie7){
    		top.resizeTo(810,742);
    	}else{
    		top.resizeTo(810,719);
    	}
    	document.getElementById("flash-section").style.display = 'block'; 
    	document.getElementById("backtomain").style.display = 'none'; 
    }else{
    	if(navigator.appName == "Netscape"){
    		lastDims[0] = top.outerWidth;
    		lastDims[1] = top.outerHeight;
    	}
    	lastpos[0] = (document.all)?window.screenLeft:window.screenX;
    	lastpos[1] = (document.all)?window.screenTop:window.screenY;
    	dims=[200,140];
    	pos=[screen.width-dims[0],screen.height-dims[1]];
    	top.resizeTo(dims[0],dims[1]);
    	self.moveTo(pos[0],pos[1]);
    	toggled = true;
    	document.getElementById("flash-section").style.display = 'none'; 
    	document.getElementById("backtomain").style.display = 'block'; 
    }
}


/* Casino - dropdown menu
------------------------------------- */
	
var casino_timeout;
jQuery(document).ready(function(){

// Over button
jQuery(".listbutton a").mouseover(function() {
    set_dropdown_position = jQuery(this).parent().parent().parent().height();
    
    jQuery("div.listitem div.menu-dropdown").css("top", set_dropdown_position-1);
    
    casino_dropdown = jQuery(this).parent().parent().parent().parent().find(".menu-dropdown");
    	
    if(casino_dropdown && casino_dropdown.find("li").length > 0)    //dont show if no variants
        casino_dropdown.show();
        
    clearTimeout(casino_timeout);
});

// Leave button
jQuery(".listbutton a").bind("mouseleave",function() {		
    casino_timeout = setTimeout(function() {
    	if(casino_dropdown){
    	    casino_dropdown.hide();
    	}
    }, 30); 
});

// Over menu
jQuery("div.listitem div.menu-dropdown").mouseover(function() {		
    clearTimeout(casino_timeout);		
});

// Leave menu
jQuery("div.listitem div.menu-dropdown").bind("mouseleave",function() {
    casino_dropdown.hide();
    clearTimeout(casino_timeout);
});

})