

var akcesoria = new function(){
    var _this = this;
//Kac modification to try redirect
if (window.location.hash == "#Podróż"){
	window.location.href = "/podroz";
}
if (window.location.hash == "#Konfigurator_akcesoriów"){
	window.location.href = "/konfigurator";
}
if (window.location.hash == "#Showroom"){
	window.location.href = "/hitec";
}
if (window.location.hash == "#Akcesoria_off_road"){
	window.location.href = "/offroad";
}
if (window.location.hash == "#Haki"){
	window.location.href = "/haki";
}
if (window.location.hash == "#Praktyczność"){
	window.location.href = "/praktycznosc";
}
if (window.location.hash == "#12"){
	window.location.href = "/pakiety";
}
if (window.location.hash == "#konfigurator"){
	window.location.href = "/konfigurator";
}
if (window.location.hash == "#Foteliki_dla_dzieci"){
	window.location.href = "/foteliki";
}
if (window.location.hash == "#System_audio_JBL"){
	window.location.href = "/jbl";
}
//end of kac modification
    this.baseTitle = '';
    this.currentApplicationData = null;
    
    this.applicationContainer = null;
    this.lowerOriginalHeight = 0;
    
    this.init = function(){
    };
    
    this.onLoad = function(){
        _this.baseTitle = document.title;
        _this.lowerOriginalHeight = $('#lower').height();

	preloader.init(function(){
	
		if (!_this.handleExistingAnchor())
	    	_this.resetUrlAndTitle.call(_this);
		});
	sound.init();
	social.init();
	promo.init();
	
	    
	_this.prepareHashHandler();
    };
    
    this.showApplication = function(applicationData){
        if (this.currentApplicationData)
            this.removeApplication();
        this.currentApplicationData = applicationData;
	this.setUrlAndTitle();
        this.embedApplication();
    };
    
    this.setUrlAndTitle = function(){
        this.setTitle();
        this.setUrl();
	social.setSocialButtons('#' + this.currentApplicationData.category.rewrite + '/' +this.currentApplicationData.rewrite,this.baseTitle + ' - ' + this.currentApplicationData.name);
    };
    
    this.resetUrlAndTitle = function(){
        this.resetTitle();
        this.resetUrl();
	social.setSocialButtons('/', this.baseTitle);
    };    
    
    this.setTitle = function(){
        document.title = this.baseTitle + ' - ' + this.currentApplicationData.name;  
    };
    
    this.resetTitle = function(){
        document.title = this.baseTitle;  
    };    
    
    this.setUrl = function(){
	this.supressHashHandler = true;
	_this.lastHash = '#' + this.currentApplicationData.category.rewrite + '/' +this.currentApplicationData.rewrite;
        window.location = _this.lastHash;
    };
    
    this.resetUrl = function(){
	this.supressHashHandler = true;	
        //window.location = '#';
    };    
    
    this.removeApplication = function(){
	if (! this.currentApplicationData)
	    return;
        this.resetApplicationSpace();
        this.currentApplicationData = null;
	this.changeBackground('');
	
        if (this.applicationContainer){
            this.applicationContainer.hide();
            this.applicationContainer.remove();            
        }
        this.applicationContainer = null;
        
        if (this.setCssFile(null)){
            $('#sitebg').hide();
	    setTimeout(function(){
            		$('#sitebg').hide();
	    	}, 1100);
        }
        else{
            $('#sitebg').show();
        }
        this.resetUrlAndTitle();
	//console.log("sound.lastState %s", sound.lastState);
	if (sound.lastState != 'play')
	    sound.play(5);
    };    
    
    this.setApplicationSpace = function(){
        var appHeight = this.currentApplicationData.height;
        var availHeight = $('#upper').height() - $('#mainmenu').height() - this.lowerOriginalHeight;
        if (appHeight > availHeight) {
            var missingHeight = appHeight - availHeight;
            var lowerSubstraction = missingHeight < this.lowerOriginalHeight ? missingHeight : this.lowerOriginalHeight;
            
            //$('#lowertop').fadeOut(300, function(){
            //        $('#lower').animate({height: '-='+lowerSubstraction}, 1000);
            //    });
            $('#lowertop').hide();
            $('#lower').stop(true,true).animate({height: '-='+lowerSubstraction}, 1000);	    
            
        }
    };
    
    this.resetApplicationSpace = function(){
	$('#lower').stop(true,true).animate({height: this.lowerOriginalHeight}, 1000);
	$('#lowertop').show();
	
    };
    
    this.embedApplication = function(){
        this.setApplicationSpace();
        if (this.setCssFile(this.currentApplicationData.css)){

	    setTimeout(function(){
            		$('#sitebg').hide();
	    	}, 1100);
            $('#sitebg').hide();
        }
        else{
            $('#sitebg').show();
        }
        
        if (this.currentApplicationData.has_content != '0')
            this.embedApplicationContent();
        else
            this.embedApplicationFlash();
	    
        if (this.currentApplicationData.sound == '1')
            sound.pause(5);
	else if (sound.lastState != 'play')
	    sound.play(5);
	    
	promo.promoHide();
    };
    
    this.embedApplicationContent = function(){
	this.traceGA(this.currentApplicationData.name, 'Start');
        this.prepareHTMLContainer();
	$('div.fl', this.applicationContainer).html(this.currentApplicationData.content);
	if (this.currentApplicationData.movie){
	    this.initFlashValues();
	    if (! $('#sm-main', this.applicationContainer).length){
		$('div.fl', this.applicationContainer).prepend('<div id="sm-main" />');
	    }
	    this.embedFlash('sm-main');    	    
	}
    };
    
    this.prepareHTMLContainer = function(){
	var div = $('<div></div>').attr('id', 'flo' + this.currentApplicationData.id);
	div.addClass('fl');
	div.css('width', this.currentApplicationData.width + 'px');
	div.css('height', this.currentApplicationData.height + 'px');
        this.applicationContainer = $('<div id="html_flash_container"><div id="html_box"></div><div id="scroll"></div></div>');
	$('#html_box',this.applicationContainer).append(div);
	$('#html_box',this.applicationContainer).css(
	    {width:this.currentApplicationData.width + 'px',
	    height:this.currentApplicationData.height + 'px'
	});
	this.applicationContainer .css(
	    {width: (parseInt(this.currentApplicationData.width) + 40 + 30) + 'px'
	});	
        $('#container').append(this.applicationContainer); 	
    };
    
    this.embedApplicationFlash = function(){
        //alert('unimplemented');
        this.traceGA(this.currentApplicationData.name, 'Start');
        this.prepareContainer();
        this.initFlashValues();
        this.embedFlash();
    };
    
    this.traceGA = function(name,action){
        if (_gaq)
	{
            _gaq.push(['_trackEvent', name, action]);
            _gaq.push(['_trackPageview', '/' + name]); 
	}        
    };
    
    this.prepareContainer = function(){
	var div = $('<div>' + this.currentApplicationData.name + '</div>').attr('id', 'flo' + this.currentApplicationData.id);
	div.addClass('fl');
	div.css('width', this.currentApplicationData.width + 'px');
	div.css('height', this.currentApplicationData.height + 'px');
        this.applicationContainer = $('<div id="flash_container" />').append(div);
        $('#container').append(this.applicationContainer);
        this.applicationContainer.css({width: this.currentApplicationData.width + 'px',
                                       height: this.currentApplicationData.height + 'px'});
        // done by prepareContainer//
	//add by Kac
	// var up = document.getElementById("upper");
	// up.onload = function () {up.style.backgroundColor="#000000";};
	//end Kac add
    };
    
    this.params = {}
    this.flashvars = {}
    this.attributes = {}
    
    this.setParams = function(object){
        this._setParams('params', object);
    };
    this.setFlashvars = function(object){
        this._setParams('flashvars', object);
    };
    this.setAttributes = function(object){
        this._setParams('attributes', object);
    };
    
    this._setParams = function(params, object){
        if (object == null)
        {
            this[params] = {};
        }
        else
        {
            this[params] = $.extend(this[params], object);
        }
    };
    
    this.initFlashValues = function(){
        this.setAttributes(null);
        this.setFlashvars(null);
        this.setParams(null);
        
        this.setFlashvars({basedir: 'files/' + this.currentApplicationData.id + '/',
                          itemid: this.currentApplicationData.id,
                          appname: this.currentApplicationData.name,
                          scale: 'scale',
                          salign: 't',
                          xmlfile: this.currentApplicationData.xml,
                          xmladdress: this.currentApplicationData.xml,
                          sub: this.currentApplicationData.movie,
                          title: this.currentApplicationData.name});
        this.setParams({wmode: 'transparent',
                        scale: 'scale',
                        bgcolor: '#ffffff',
                        quality: 'best'})

        this.setAttributes({name: 'content_alll__flash'});        
    };
    
    this.embedFlash = function(htmlId){
	var destination =  htmlId ? htmlId : $('.fl', this.applicationContainer).attr('id');
        var t = swfobject.embedSWF(this.currentApplicationData.movie,
                           destination,
                           this.currentApplicationData.width,
                           this.currentApplicationData.height,
                           "9.0.0",
                           "expressInstall.swf",
                           this.flashvars,
                           this.params,
                           this.attributes
			   );
    };
    
    this.setCssFile = function(cssUrl){
        // <link rel="stylesheet" type="text/css" href="new/css/akcesoria.css">
        if (typeof cssUrl == "undefined" || cssUrl == null || cssUrl == ""){
	    removejscssfile($('#customstyles').attr('href'), 'css');
            return false;
        }
	
	if ($('#customstyles').length < 1)
	{
		loadjscssfile(cssUrl, 'css');
	}
	else
	{
		replacejscssfile($('#customstyles').attr('href'), cssUrl, 'css');
	}	
       // var cssLink = $('#customcss');
       // cssLink.remove();
        
       // if (typeof cssUrl == "undefined" || cssUrl == null || cssUrl == "")
       //     return false;
        
       // cssLink = $('<link id="customcss" rel="stylesheet" type="text/css" />').attr('href', cssUrl);
       // $('head').append(cssLink);
        return true;
    };
    
    this._defBgColor = '#000000';
    
    this.changeBackground = function(bgimage,bgcolor)
    {
            $('#sitebg').show();

            if (typeof bgcolor == "undefined" && bgimage)
            {
                bgcolor = this._defBgColor;
            }
	    if (bgcolor)
		$('body').css('background-color', bgcolor);
	    else
		$('body').css('background-color', '');
	    
            //$('#background').fadeOut(500, function(){
	    if (bgimage)
                    $('#sitebg').css('background-image', 'url('+bgimage+')');
            //	$('#background').fadeIn(500);
            //	});
            //_onResize();
    }
    
    this.handleExistingAnchor = function(){
	var hashes = window.location.hash.replace('#','').split('/', 2);
	if (hashes.length == 0){
	    _this.removeApplication.call(_this);
	    return false;
	}
	var appPart = '';
	if (hashes.length == 2)
	    appPart = hashes[1];
	else
	    appPart = hashes[0];
	    
	var application_id = parseInt(appPart);
	
	if (!application_id){
	    var application = menu.getAppByPermalink(appPart);
	    if (!application)
		return false;
	    application_id = application.id;
	}	
	return _this.showAppById(application_id);
    };
    
    this.showAppById = function(appId){
	var application = menu.getAppById(appId);
	if (!application)
	    return false;
	
	var li = $('a[href=#'+application.category.rewrite+'/]').parent();
	li.data('fade_disable', true);
	li.click();
	menu.hideTopMenuItem(li);
	
	_this.showApplication(application);
	return true;
    };
    
    this.prepareHashHandler = function(){
	$(window).hashchange(_this.onHashChange);	
    };
    
    this.supressHashHandler = false;
    this.lastHash = '';
    this.onHashChange = function(e){
	//console.log(e);
	//console.log(_this.supressHashHandler);
	//console.log(window.location.hash);
	//if (! _this.supressHashHandler){
	    if (window.location.hash != _this.lastHash){
		_this.handleExistingAnchor();
		_this.lastHash = window.location.hash;
	    }
	//}
	//else
	//{
	//   _this.supressHashHandler = false;
	//}
	
    };
    
}

$(akcesoria.onLoad);
changeBackground = akcesoria.changeBackground;










