// Class Showcase
var Showcase = {
  navigationWrap : window.document.getElementById( "soNavigation" ),
  contentWrap : window.document.getElementById( "soContent" ),
  iframe : undefined,
  navigationLoadingFlag : 0,
  contentLoadingFlag : 0,
  navigationHTML: undefined,
  contentHTML: undefined,
  outputInterval : undefined,
  currentURI : undefined,
  nRequest : 1,

  baseURI : "http://mob.smsonline.ru/",
  iframeURI : "http://mob.smsonline.ru/?a=iframe",
	navigationURI : "http://mob.smsonline.ru/?a=navigation",
	cssURI : "http://mob.smsonline.ru/?a=css",
  counterURI : "http://mob.smsonline.ru/?a=stat",
  pageBaseURI : undefined,

////
  createIframe : function(){  
    var iframe = window.document.createElement( "iframe" );

    if ( Showcase.currentURI.match( /#/ ) ){  
      iframe.src = Showcase.iframeURI + "&uri=" + encodeURIComponent( Showcase.currentURI );
    }
    /*else{
      iframe.src = Showcase.iframeURI + "&uri=" + encodeURIComponent( Showcase.currentURI 
          + '#main/' );
    }*/

    iframe.style.position = "absolute";
    iframe.style.top = "-100px";
    iframe.style.left = "-100px";
    iframe.style.visibility = 'hidden';
    iframe.style.width = "1px";
    iframe.style.height = "1px";
    Showcase.iframe = iframe;
    Showcase.navigationWrap.parentNode.appendChild( iframe );
  },

////
  attachCSS : function(){
    head = window.document.getElementsByTagName( "head" )[ 0 ];
    var link = window.document.createElement( "link" );
    link.rel = "stylesheet";
    link.type = "text/css";
    link.href = Showcase.cssURI;
    head.appendChild( link );
  },

////
  callback : function( stat, callbackObj, data ){  
    if ( stat == 0 ){
      data = "Can't load data";
    }

    if ( callbackObj.id == "soNavigation" ){
      Showcase.navigationHTML = data;
      Showcase.navigationLoadingFlag = 1;
    }
    else if ( callbackObj.id == "soContent" ){
      Showcase.contentHTML = data;
      Showcase.contentLoadingFlag = 1;
    }
    else{
      callbackObj.innerHTML = data;
    }
  },

////
  progress : function( callbackObj ){
    var progressBarBg = window.document.createElement( "div" );
    progressBarBg.id = "soProgressBarBg";
    var progressBar = window.document.createElement( "div" );
    progressBar.id = "soProgressBar";
    callbackObj.appendChild( progressBarBg );
    callbackObj.appendChild( progressBar );
  },

////
  selectCountry : function(){
    var selectCountryField = window.document.getElementById( "soSelectCountryField" );
    cncode = selectCountryField.options[ selectCountryField.selectedIndex ].value;
    var parameters;

    if ( window.location.hash ){
      parameters = Showcase.splitParam();
   
      if ( parameters.op ){
        parameters.op = undefined;
      }
    }
    else{
      parameters = new Array;
    }

    Showcase.nRequest = 1;
    parameters.cn = cncode;
    window.location.hash = Showcase.joinParam( parameters );
  },

////
  selectOperator : function(){
    var selectOperatorField = window.document.getElementById( "soSelectOperatorField" )
    operator = selectOperatorField.options[ selectOperatorField.selectedIndex ].value;
    var parameters;

    if ( window.location.hash ){
      parameters = Showcase.splitParam();
    }
    else{
      parameters = new Array;
    }

    parameters.op = operator;
    window.location.hash = Showcase.joinParam( parameters );
  },

////
  onChangeURI : function(){
    if ( Showcase.currentURI && ( Showcase.currentURI != window.location.href ) ){
      Showcase.request( Showcase.splitParam() );
      Showcase.currentURI = window.location.href;
    }
  },

////
  request : function( parameters ){
  	
  	if ( Showcase.readCookie( 'uncensored' ) ) {
  		parameters.ad = 1;
  	}
  	
    scriptHttpRequest.get( Showcase.baseURI, Showcase.contentWrap, Showcase.callback, 
      { progress: Showcase.progress, parameters: parameters } );

    if ( Showcase.nRequest ){

      if ( parameters && ( typeof( parameters ) == "object" ) ){

        if ( parameters.a ){
          scriptHttpRequest.get( Showcase.navigationURI, Showcase.navigationWrap, 
            Showcase.callback, { parameters: { ct: parameters.ct, g: parameters.g, 
                cn: parameters.cn, op: parameters.op } } );  
        }
        else{
          scriptHttpRequest.get( Showcase.navigationURI, Showcase.navigationWrap, 
            Showcase.callback, { parameters: { cn: parameters.cn, op: parameters.op } } ); 
        }
      }
      else{
        scriptHttpRequest.get( Showcase.navigationURI, Showcase.navigationWrap, Showcase.callback ); 
      }
    }
    else{
      Showcase.navigationLoadingFlag = 1;
    }

    Showcase.outputInterval = setInterval( Showcase.output, 1 );
  },

////
  output : function(){
    if ( ( Showcase.navigationLoadingFlag == 1 ) && ( Showcase.contentLoadingFlag == 1 ) ){
      clearInterval( Showcase.outputInterval );
      Showcase.navigationWrap.innerHTML = Showcase.navigationHTML;
			Showcase.contentWrap.innerHTML = Showcase.contentHTML;
      Showcase.processHref( Showcase.navigationWrap );
      Showcase.processHref( Showcase.contentWrap );
      window.scroll( 0, 0 );
      Showcase.navigationLoadingFlag = 0;
      Showcase.contentLoadingFlag = 0;
    }
  },

////
  confirmedAdult : function(){
  	Showcase.createCookie ( 'uncensored', 1 );
  	var origParams = Showcase.splitParam();
  	origParams.ad = 1;
  	Showcase.request( origParams );
  },

////
  processHref : function( obj ){
    var links = obj.getElementsByTagName( 'a' );
    
    for ( var i = 0; i < links.length; i++ ){

      if ( links[ i ].href.match( /#/ ) ){
        links[ i ].href = Showcase.pageBaseURI + links[ i ].hash;
        
        ( function( i ){ 
            Showcase.addEventListenerToObj( links[ i ], "click", function( evt ){ 
                Showcase.openLink( evt, links[ i ].href ) } ); 
        } )( i );
      }
    }
  },

////
  openLink : function( evt, link ){
    Showcase.preventDefaultAction( evt );

    if ( navigator.userAgent.indexOf( "MSIE" ) >= 0 ){
			Showcase.iframe.src = Showcase.iframeURI + "&uri=" 
					+ encodeURIComponent( link );
    } 
    else{
      window.location.href = link;
    }
  },

////
	getEvent : function( evt ){
		return evt ? evt : window.event;
	},

////
	addEventListenerToObj : function ( obj, eventType, handler ){

		if ( obj.attachEvent ){
			obj.attachEvent( "on" + eventType, handler );
		}
		else if ( obj.addEventListener ){
			obj.addEventListener( eventType, handler, false );
		}
	},

////
	preventDefaultAction : function( evt ){
		eventObj = Showcase.getEvent( evt );

		if ( eventObj ){

			if( eventObj.preventDefault ){
				eventObj.preventDefault();
			}
			else{
			 eventObj.returnValue = false;
			}
		}
	},

////
  removeAllChild : function( node ){
    while ( node.hasChildNodes() ){
      node.removeChild( node.childNodes[0] );
    }
  },

////
  splitParam : function(){
    var hash = window.location.hash;
    hash = hash.replace( /\/$/, "" );
    hash = hash.replace( /^#/, "" );
    var hashParts = hash.split( "/" );

    if ( !hashParts[ 2 ] || !Showcase.navigationHTML || ( hashParts[ 2 ] == "nr:1" ) ){
      Showcase.nRequest = 1
    }
    else{
      Showcase.nRequest = 0;
    }
   
    if ( hashParts[ 0 ] ){
      var parameters = new Array;
  
      if ( ( hashParts[ 0 ] == "list" ) || ( hashParts[ 0 ] == "show" ) 
          || ( hashParts[ 0 ] == "authors" ) ){

        parameters[ "a" ] = "content:" + hashParts[ 0 ];
      }
  
      if ( hashParts[ 1 ] ){
        var pairs = hashParts[ 1 ].split( ";" );

        for ( var i = 0; i < pairs.length; i++ ){
          var param = pairs[ i ].split( ":" );

          if ( param[ 1 ] ){
            parameters[ param[ 0 ] ] = param[ 1 ];
          }
        }
      }
 
      return parameters;
    }
    else{
      return false;
    }
  },

////
  joinParam : function( parameters ){
    var paramString;

    for ( key in parameters ){
       
      if ( ( key != "a" ) && parameters[ key ] ){

        if ( parameters[ key ] instanceof Array ){

          for ( var i = 0; i < parameters[ key ].length; i++ ){

            if ( paramString ){
              paramString += ";" + key + ":" + parameters[ key ][ i ];
            } 
            else{
              paramString = key + ":" + parameters[ key ][ i ];
            } 
          }
        }
        else{

          if ( paramString ){
            paramString += ";" + key + ":" + parameters[ key ];
          } 
          else{
            paramString = key + ":" + parameters[ key ];
          } 
        }
      }  
    }

    if ( parameters.a ){
      parameters.a = parameters.a.replace( /^content:/, "" );
      paramString = parameters.a + "/" + paramString;
    }
    else{
      paramString = "main/" + paramString;
    }

    if ( !Showcase.nRequest ){
      paramString += "/nr:0/";
    }

    return paramString;
  }, 

////
  turnPage : function turnPage( evt ){
    var event = evt ? evt : window.event;
  
    var tpageObj;
    if ( event.keyCode == 37 ){

      if ( event.ctrlKey ){

        if ( tpageObj = window.document.getElementById( "turnPageBack" ) ){
          window.location.href = tpageObj.href;
        }

        Showcase.disableDefaultAction( event );
      }
    }
    else if ( event.keyCode == 39 ){

      if ( event.ctrlKey ){

        if ( tpageObj = window.document.getElementById( "turnPageForward" ) ){
          window.location.href = tpageObj.href;
        }

        Showcase.disableDefaultAction( event );
      }
    }
  },

////
  disableDefaultAction : function disableDefaultAction( event ){

    if (event){

      if(event.preventDefault){
        event.preventDefault();
      }
      else{
        event.returnValue = false;
      }
    }
  },

////
  createCookie : function( name, value, days ){

    if ( days ){
      var date = new Date();
      date.setTime( date.getTime() + ( days * 24 * 60 * 60 * 1000 ) );
      var expires = "; expires=" + date.toGMTString();
    }
    else{ 
      var expires = "";
    }
 
    document.cookie = name + "=" + value + expires + "; path=" + window.location.pathname;
  },

////
  readCookie : function( name ){
    var pairs = document.cookie.split( /;\s*/ );

    for( var i = 0; i < pairs.length; i++ ){
      var re = new RegExp( "^" + name + "=" );

      if ( pairs[ i ].match( re ) ){
        return ( pairs[ i ].split( /=/ ) )[ 1 ];
      }
    }
    return false;
  },

////
  eraseCookie : function( name ){
    createCookie( name, "", -1 );
  },

////
  counterCallback : function(){
    Showcase.createCookie( "soC", 1, 1 );
  }
};


// Class scriptHttpRequest
var scriptHttpRequest = {

  Transaction : function(){
    this.data;
    this.script;
    this.interval;
    this.timeout;
   
    return this;
  },
 
  transactions : new Array,
  head : window.document.getElementsByTagName( "head" )[ 0 ],

////
  get : function( url, callbackObj, callback, options ){

    if ( options && options.progress ){
      options.progress( callbackObj );
    }

    var tid;

    if ( options && options.tid ){
      tid = options.tid;  
      scriptHttpRequest.finalizeTransaction( tid );
      scriptHttpRequest.clearTransaction( tid );
    }
    else{

      do {
        tid = parseInt( Math.random() * 1000000 );
      } 
      while ( scriptHttpRequest.transactions[ tid ] );
    }

    if ( url.match( /\?/ ) ){
      url += "&";
    }
    else{
      url += "?";
    }

    url += "tid=" + tid + "&";
   
    if ( options && options.parameters && ( typeof( options.parameters ) == 'object' ) ){
      var paramString = scriptHttpRequest.joinParam( options.parameters );

      if ( paramString ){
        url += paramString + "&";
      }
    }

    if ( !options || !options.cache || options.cache == 0 ){
      url += "rand=" + ( Math.random() * 1000000 );
    }

    scriptHttpRequest.transactions[ tid ] = new scriptHttpRequest.Transaction;
    scriptHttpRequest.transactions[ tid ].script = window.document.createElement( "script" );
    scriptHttpRequest.transactions[ tid ].script.type = "text/javascript";
    scriptHttpRequest.transactions[ tid ].script.src = url;
    scriptHttpRequest.head.appendChild( scriptHttpRequest.transactions[ tid ].script );

    scriptHttpRequest.transactions[ tid ].interval = setInterval( function(){ 
        scriptHttpRequest.handler( tid, function(){ callback( 1, callbackObj, 
        scriptHttpRequest.transactions[ tid ].data ) } ) }, 1 );

    if ( options && options.timeout ){
      scriptHttpRequest.transactions[ tid ].timeout = setTimeout( function(){ 
          scriptHttpRequest.abort( tid, function(){ callback( 0, callbackObj, 
          scriptHttpRequest.transactions[ tid ].data ) } ) }, options.timeout ); 
    }
  },

////
  handler : function( tid, callback ){
  
    if ( scriptHttpRequest.transactions[ tid ].data ){
      scriptHttpRequest.finalizeTransaction( tid );
      callback();
      scriptHttpRequest.clearTransaction( tid );
    }
  },

////
  abort : function( tid, callback ){
    scriptHttpRequest.abortTransaction( tid );
    callback();
    scriptHttpRequest.clearTransaction( tid );
  },

////
  set : function( tid, data ){
    if ( scriptHttpRequest.transactions[ tid ] ){
      scriptHttpRequest.transactions[ tid ].data = data;
    }
  },

////
  joinParam : function( parameters ){
    var paramString;

    for ( key in parameters ){
 
      if ( parameters[ key ] ){

        if ( parameters[ key ] instanceof Array ){

          for ( var i = 0; i < parameters[ key ].length; i++ ){

            if ( paramString ){
              paramString += "&" + key + "=" + encodeURIComponent( parameters[ key ][ i ] );
            } 
            else{
              paramString = key + "=" + encodeURIComponent( parameters[ key ][ i ] );
            } 
          }
        }
        else{

          if ( paramString ){
            paramString += "&" + key + "=" + encodeURIComponent( parameters[ key ] );
          } 
          else{
            paramString = key + "=" + encodeURIComponent( parameters[ key ] );
          } 
        }  
      }
    }

    return paramString;
  },

////
  abortTransaction : function( tid ){
    if ( scriptHttpRequest.transactions[ tid ] ){
      clearInterval( scriptHttpRequest.transactions[ tid ].interval );
      scriptHttpRequest.head.removeChild( scriptHttpRequest.transactions[ tid ].script );
    }
  },

////
  finalizeTransaction : function( tid ){
    if ( scriptHttpRequest.transactions[ tid ] ){
      clearTimeout( scriptHttpRequest.transactions[ tid ].timeout );
      clearInterval( scriptHttpRequest.transactions[ tid ].interval );
      scriptHttpRequest.head.removeChild( scriptHttpRequest.transactions[ tid ].script );
    }
  },

////
  clearTransaction : function( tid ){
    scriptHttpRequest.transactions[ tid ] = undefined;
  }
};

// Class soFuncs
var soFuncs = {
  showList : function( item, itemClassName1, itemClassName2 ){
		if (item.parentNode.className == itemClassName1) {
   		item.parentNode.className = itemClassName2;
   	}
   	else if (item.parentNode.className == itemClassName2) {
   		item.parentNode.className = itemClassName1;
   	}
    return false;
  },

  openWindow : function( urlLink, win_width, win_height, resizable, scrollbars ){
  	resizable = resizable ? 1 : 0;
  	scrollbars = scrollbars ? 1 : 0;
  	var soWind = window.open(urlLink,'smshelp','resizable='+resizable+',scrollbars='+scrollbars+',width='+win_width+',height='+win_height);
  	soWind.focus();
  	return false;
  }
};

Showcase.pageBaseURI = window.location.href;
Showcase.pageBaseURI = Showcase.pageBaseURI.replace( /#.*$/, "" );

var reqString;

if ( soOptions && soOptions.partnerId ){
  Showcase.navigationURI += "&pid=" + soOptions.partnerId;
  Showcase.counterURI += "&pid=" + soOptions.partnerId;
	reqString = "&pid=" + soOptions.partnerId;
}

Showcase.counterURI += "&uri=" + encodeURIComponent( Showcase.pageBaseURI + ( reqString ? reqString.replace( /^\&*/, "?" ) : "" ) );

if ( soOptions && soOptions.skin ){
  soOptions.skin = soOptions.skin.replace( /^\s+/, "" );
  soOptions.skin = soOptions.skin.replace( /\s+$/, "" );
	Showcase.navigationURI += "&sk=" + soOptions.skin;
  Showcase.cssURI += "&sk=" + soOptions.skin;
	reqString += "&sk=" + soOptions.skin;
}

if ( soOptions && soOptions.unicode ){
	Showcase.navigationURI += "&u=" + 1;
	reqString += "&u=" + 1;
}

if ( soOptions && typeof( soOptions.categoryFilter ) == "object" ){
	var tmp = "";
	for ( var i = 0; i < soOptions.categoryFilter.length; i++ ) {
		tmp += soOptions.categoryFilter[i] + ",";
	}
	if ( tmp ) {
		tmp = tmp.substr( 0, tmp.length - 1 );
		Showcase.navigationURI += "&cf=" + tmp;
		reqString += "&cf=" + tmp;
	}
}

if ( soOptions && typeof( soOptions.genreFilter ) == "object" ){
	var tmp = "";
	for ( var i = 0; i < soOptions.genreFilter.length; i++ ) {
		tmp += soOptions.genreFilter[i] + ",";
	}
	if ( tmp ) {
		tmp = tmp.substr( 0, tmp.length - 1 );
		Showcase.navigationURI += "&gf=" + tmp;
		reqString += "&gf=" + tmp;
	}
}

if ( soOptions && typeof( soOptions.countriesFilter ) == "object" ){
  var tmp = "";
  for ( var i = 0; i < soOptions.countriesFilter.length; i++ ) {
    tmp += soOptions.countriesFilter[ i ] + ",";
  }
  if ( tmp ) {
    tmp = tmp.substr( 0, tmp.length - 1 );
    Showcase.navigationURI += "&cnf=" + tmp;
    reqString += "&cnf=" + tmp;
  }
}

if ( reqString ){
	reqString = reqString.replace( /^\&*/, "?" );
  Showcase.baseURI += reqString;
}

Showcase.attachCSS();

if ( !window.location.href.match( /#/ ) ){  
  window.location.hash = 'main';
}

Showcase.currentURI = window.location.href;

if ( navigator.userAgent.indexOf("MSIE") >= 0 ){
	Showcase.createIframe();
}


if ( !Showcase.readCookie( 'soC' ) ){
  scriptHttpRequest.get( Showcase.counterURI, undefined, Showcase.counterCallback );
}

Showcase.request( Showcase.splitParam() ); 
setInterval( Showcase.onChangeURI, 1 );

window.document.onkeydown = Showcase.turnPage;

