/**
 * jget plugin
 * @author Alexandre Magno
 * @desc get a query string to be accessible for javascript
 * @version 1.0
 * @example
 *
 * http://www.foo.com/test.php?var1=test1
 * var outputQuery = $.jget['var1'];
 * $.debug(outputQuery);
 * //Will show 'test1'
 *
 *
 * @license free
 * @param bool vertical, bool horizontal
 * @site http://blog.alexandremagno.net
 *
 */

jQuery.extend({

	//starting the jget object
	jget: {},
	//get the url
	url: window.location.href.replace(/^[^\?]+\??/,''),
	//get the queryString
	parseQuery: function ( query ) {
	   var Params = {};
	   if ( ! query ) {return Params;}// return empty object
	   var Pairs = query.split(/[;&]/);
	   for ( var i = 0; i < Pairs.length; i++ ) {
	      var KeyVal = Pairs[i].split('=');
	      if ( ! KeyVal || KeyVal.length != 2 ) {continue;}
	      var key = unescape( KeyVal[0] );
	      var val = unescape( KeyVal[1] );
	      val = val.replace(/\+/g, ' ');
	      Params[key] = val;
	   }
	   return Params;
	},
	//Make the jget object available to jQuery.extend
	getQueryString: function() {
		this.jget = this.parseQuery(this.url);
	},
	//debugging
	debug: function(message) {

		if(!$.browser.msie) {

			console.info(message);

		} else if($.browser.safari) {

			window.console.log(message);

		}

	}

});

//start the plugin
$.getQueryString();

//default vars
var selectedLocale = '';


////////////////////////////////////////////
////// FLASH SHIZZLE

	function thisMovie(movieName)
	{
		if (navigator.appName.indexOf("Microsoft") != -1)
		{
			return window[movieName];
		}
		else
		{
			return document[movieName];
		}
	}


////////////////////////////////////////////
////// OVERLAY SHIZZLE

	function underscoreLocale(locale) {
		var returnedLocale = locale.replace('-','_');
		return returnedLocale;
	}

	function hyphenLocale(locale) {
		var returnedLocale = locale.replace('_','-');
		return returnedLocale;
	}


////////////////////////////////////////////
////// OVERLAY SHIZZLE

	function discreet_openBadge() {

		$("body").prepend('<div id="overlay"><img src="images/close-button.png" id="close" alt="close" /><div id="badge"></div></div>');

		var badgeHeight = "180";
		var badgeWidth = "215";

		var flashvars = {
		
		// these parameters are required for badge install:
			"airversion": "1.5.3", // version of AIR runtime required
			"appname": "Discreet Admirer", // application name to display to the user
			"appurl": "http://www.discreet.pl/assets/air/" + hyphenLocale(selectedLocale) + "/BeAdmired.air", // absolute URL (beginning with http or https) of the application ".air" file
		
		// these parameters are required to support launching apps from the badge (but optional for install):
			"appid": "BeAdmired", // the qualified application ID (ex. com.gskinner.air.MyApplication)
			"pubid": "", // publisher id
		
		// this parameter is required in addition to the above to support upgrading from the badge:
			"appversion": "1.00", // AIR application version
		
		// these parameters are optional:
			"image": "assets/badge/badgeimage.jpg", // URL for an image (JPG, PNG, GIF) or SWF to display in the badge (205px wide, 170px high)
			"appinstallarg": "installed from web", // passed to the application when it is installed from the badge
			"applauncharg": "launched from web", // passed to the application when it is launched from the badge
			"helpurl": "help.html", // optional url to a page containing additional help, displayed in the badge's help screen
			"hidehelp": "false", // hides the help icon if "true"
			"skiptransition": "false", // skips the initial transition if "true"
			"titlecolor": "#d48200", // changes the color of titles
			"buttonlabelcolor": "#FFFFFF", // changes the color of the button label
			"appnamecolor": "#FFFFFF", // changes the color of the application name if the image is not specified or loaded
		
		// these parameters allow you to override the default text in the badge:
			"str_error": badgeLocale.str_error,
			"str_err_params": badgeLocale.str_err_params, 
			"str_err_airunavailable": badgeLocale.str_err_airunavailable, 
			"str_err_airswf": badgeLocale.str_err_airswf, 
			"str_loading": badgeLocale.str_loading, 
			"str_install": badgeLocale.str_install, 
			"str_launch": badgeLocale.str_launch, 
			"str_upgrade": badgeLocale.str_upgrade, 
			"str_close": badgeLocale.str_close, 
			"str_tryagain": badgeLocale.str_tryagain, 
			"str_launching": badgeLocale.str_launching, 
			"str_launchingtext": badgeLocale.str_launchingtext, 
			"str_installing": badgeLocale.str_installing, 
			"str_installingtext": badgeLocale.str_installingtext, 
			"str_help": badgeLocale.str_help, 
			"str_helptext": badgeLocale.str_helptext, 
			"str_err_airswf": badgeLocale.str_err_airswf 
		
		};
		
		var params = {
		};
		
		var attributes = {
            id: 'badge',
        	name: 'badge'
		};
		
		swfobject.embedSWF("assets/badge/AIRInstallBadge.swf", "badge", badgeWidth, badgeHeight, "9.0.0", "assets/badge/expressInstall.swf", flashvars, params, attributes);


	}

	function discreet_closeBadge()
	{
		$("#overlay").fadeOut(2000, function () {
			$(this).remove();
		});
	}

	function discreet_downloadClicked()
	{
		thisMovie("website").tellFlashWeAreClosed();
	}
    



////////////////////////////////////////////
////// PAGE WORK SHIZZLE


	var viewportwidth = 0;
	var viewportheight = 0;
 
	function _getViewport() {

		// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
		if (typeof window.innerWidth != 'undefined')
		{
			viewportwidth = window.innerWidth;
			viewportheight = window.innerHeight;
		}

		// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
		else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0)
		{
			viewportwidth = document.documentElement.clientWidth;
			viewportheight = document.documentElement.clientHeight;
		}

		else
		{
			viewportwidth = document.getElementsByTagName('body')[0].clientWidth;
			viewportheight = document.getElementsByTagName('body')[0].clientHeight;
		}
	}


	function _realignShizzle() {
	
		_getViewport();

		var minWidth = 1200;
		var minHeight = 545;

		if (viewportheight < minHeight) {
			viewportheight = 550
		}

		var pullLeft = viewportwidth-minWidth;
		if (pullLeft < -400) { pullLeft = -400; }

		var contentHeight = (viewportheight-25);
		if (contentHeight < minHeight) { contentHeight = minHeight; }

		$('object#website').css({
			height: (viewportheight-50) + 'px'
		});


		$("#stats").text(contentHeight+'px');


		// CONTENT SURROUND
		$('#content-surround').css({
			width: '100%'
		});
		
		$('#content').css({
			height: (contentHeight)+'px'
		});
		
		if (viewportwidth > minWidth && viewportheight > minHeight) {
			$('#content-surround').css({
				backgroundPosition: 'center center', 
				marginBottom: '35px'
			});
		
		} else if (viewportwidth > minWidth && viewportheight < minHeight) {
			$('#content-surround').css({
				backgroundPosition: 'center -20px', 
				marginBottom: '0px'
			});
		} else if (viewportwidth < minWidth && viewportheight > minHeight) {

			$('#content-surround').css({
				backgroundPosition: pullLeft+'px center'
			});
		} else {
			$('#content-surround').css({
				backgroundPosition: pullLeft+'px -20px', 
				marginBottom: '35px'
			});
		}


		// ERRORS

		var errorMargin = (((contentHeight-minHeight)/2)+60);
		if (contentHeight < minHeight) { errorMargin = 60; }
		
		if (viewportheight > minHeight) {
			$('#errors').css({
				marginTop: errorMargin + 'px', 
				marginLeft: '50px' 
			});
			$('#take-your-effect').css({
				marginTop: (errorMargin-105) + 'px', 
				marginLeft: '-100px'
			});
		
		} else {
			$('#take-your-effect').css({
				top: '30px', 
				marginLeft: '-100px'
			});
		}


	}



	$(document).ready(function () {

			_getViewport();
			
			selectedLocale = $.jget['locale'];
			if (typeof selectedLocale == 'undefined') {
				//selectedLocale = 'ru_RU';				
			}
			
			//selectedLocale = $("meta[name='locale_language']").attr('content')+'_'+$("meta[name='locale_country']").attr('content');
			
			selectedLocale = 'pl_PL';

			var openBadge = '';
			openBadge = $.jget['open'];
			if (typeof openBadge != 'undefined' && openBadge == '1') {
				discreet_openBadge();
			}



            var flashvars = {
            	locale: selectedLocale
            };

            var params = {
                bgcolor: '#ffffff',
                menu: 'false',
                wmode: 'opaque', 
                allowfullscreen: 'true'
            };
            var attributes = {
                id: 'website',
                name: 'website'
		    };

			var htmlONLY = '';
			htmlONLY = $.jget['html'];
			if (typeof htmlONLY == 'undefined' || htmlONLY != '1') {
				swfobject.embedSWF('SiteBeAdmired.swf', 'discreet', '100%', (viewportheight-50), '9.0.115', 'assets/swfs/expressinstall.swf', flashvars, params, attributes);
				if ($("object#website").size() > 0) {
					$('#content, #content-surround').css({
						backgroundImage: "url('images/site/white.gif')"
					});
				}
			} else {
				$("a").each(function () {
					$(this).attr('href',$(this).attr('href')+'?html=1');
				});
			}

			_realignShizzle();

			$(window).resize(function () {
				_realignShizzle();
			});

			$("#close").live('click',function () {
				discreet_closeBadge();
			});
	
	});