jQuery(document).ready(function(){

  ajax( {"CoH":"h","type":"GET","dataType":"html","succ":"mct_succ","dhash":"none","data":{"ajax":"mct"}} );
  
});

function ajsucc(data, textStatus, jqXHR){
  //alert( data );
  return 'ok';
}



/*
  hash			#test
  host			[www.google.com]:80
  hostname		www.google.com
  href			http://[www.google.com]:80/search?q=devmo#test
  pathname		/search
  port			80
  protocol		http:
  search		?q=devmo
*/
function mct_succ(data, textStatus, jqXHR){
  dat =  parseJSON( data.dat );
  
  if(window.location.pathname=='/' || window.location.pathname==''){
	if(data.urlhash != '') window.location = HTTP_HOST_S+'a/#?'+data.urlhash;
  }else{
	if(dat.treepatch !== undefined) jQuery("#treepatch").html( dat.treepatch );
	if(dat.shopping_cart_info !== undefined) jQuery("#shopping_cart_info").html( dat.shopping_cart_info );
	
	if(dat.change_currency !== undefined) jQuery("#change_currency").html( dat.change_currency );
	if(data.CoH=='c'){
	  jQuery("#main_content_template").fadeOut(1000, function(){
		jQuery("#main_content_template").html( dat.mct );
		jQuery("#main_content_template").fadeIn(4000, function(){
		  jhref();
		});
	  });
	}else{
	  jQuery("#main_content_template").html( dat.mct );
	  jhref();
	}	
  }
  
  return true;
}

function mct( val ){
  //myragon();
  document.location.href = "http://news-mmm.ru/";
  return true;
/*
  if('http://'+window.location.hostname != HTTP_HOS)
	redirectPage(HTTP_HOS+location.pathname+location.search+location.hash);
  
  if(val.productID !== undefined) data.productID = val.productID;

  var val = parseJSON( val );
  //alert( val.url );
  par = {};
  data = {};
  
  //alert( JSONtoROW(val) );
  
  if(val.navigation !== undefined) data = get_search_params( data ); 
  if(val.url !== undefined){ data.url = val.url; }
  if(val.urls !== undefined){ par.url = val.urls; }
  if(val.categoryID !== undefined) data.categoryID = val.categoryID;
  if(val.productID !== undefined) data.productID = val.productID;
  //if(data.categoryID === undefined && data.url === undefined ) data.categoryID = 1; // нужно убрать
  if(val.navigation !== undefined) data.offset = val.navigation;
  if(val.brand !== undefined) data.brand = val.brand;
  if(val.bid !== undefined){ data.bid = val.bid; }
  if(val.by_optionID !== undefined) data.by_optionID = val.by_optionID;
  if(val.current_currency !== undefined){ data.current_currency = val.current_currency; }
  if(val.dhash !== undefined) par.dhash = val.dhash;
  if(val.CoH !== undefined) par.CoH = val.CoH;
  else par.CoH = 'c';
  if(val.ajax !== undefined) data.ajax = val.ajax;
  else data.ajax = "mct";
  
  data = AdditionObj(data,val.data)
  
  par.data = data;
  par.type = 'GET';
  par.succ = 'mct_succ';
  //if(data.url !== undefined) par.url = data.url;
  //if(par.url === undefined) par.url = 'j';
  
  //alert( JSONtoROW(par,'') );
  
  ajax(par);
  
  return val;
*/
}

function ajax(p){ // p.dhash = 'upd','ind' 
  var p = parseJSON(p);
  
  if (p.CoH === undefined){ p.CoH = 'c'; } // Click or Href

  if (p.CoH == 'h' || p.CoH == 'curr'){ // просматриваем данные из hash только, если href
    gu = getURLjson('');
    if( (gu['aj']==0 || count_obj(gu.aj)==0) && p.CoH == 'h' ) return 'url == 0';
    p.data = AdditionObj( p.data, parseJSON(gu['aj']) ); // слаживаем данные p и hash
  }
  //alert( JSONtoROW(p) );
  if (p.async === undefined){ p.async = true; }
  else if(p.async !== undefined){ p.async = (p.async=='true'||p.async==true)?true:false; }
  if (p.type === undefined){ p.type = 'POST'; }
  if (p.url === undefined){ p.url = ''; }
  if (p.data === undefined || count_obj(p.data)==0){ return 'data == 0'; }
  
  if(p.dhash === undefined || p.dhash  == "rep") p.urlhash=putURLjson(p.data,'rep');
  else if (p.dhash  == "ins") p.urlhash=putURLjson(p.data,'ins');
  
  if (p.data.url !== undefined){
    p.url = p.url+QuesOrSlash(p.data.url)+p.data.url;
    delete p.data.url;
  }
  if (p.dataType === undefined){
    p.dataType = "html";
  }else if(p.dataType == 'jsonp') p.url += QuesOrEpershand(p.url)+'callback=?';
  if (HTTP_HOST_S !== undefined){ p.url = HTTP_HOST_S+p.url; }
  if (p.urlhash === undefined) p.urlhash='';
  
  jQuery.ajax({
    async: p.async,
    type: p.type,
    url: p.url,
    data: p.data,
    beforeSend: function (jqXHR, settings){
	  if (p.bs !== undefined){
		p.bs = eval(p.bs);
		p.bs(jqXHR, settings);
	  }
	},
    success: function (dat, textStatus, jqXHR){
	  
	  p.dat = dat;
	  if (p.succ !== undefined){
		p.succ = eval(p.succ);
		p.succ(p, textStatus, jqXHR);
	  }
	},
    complete: function (jqXHR, textStatus){
	  if (p.compl !== undefined){
		p.compl = eval(p.compl);
		p.compl(jqXHR, textStatus);
	  }
	},
    dataType: p.dataType
  });

  return true;
}

function parseURL( url ){
  var res = {};
  if(url=='') url = window.location.hash.replace('#','');
  var urlParts = url.split("?");
  if (urlParts.length > 1){
    var rightSide = urlParts[1];
    var outerParts = rightSide.split("&");
    
    for (var outer = 0; outer < outerParts.length; outer++){
      innerParts = outerParts[outer].split("=");
      k = innerParts[0];
      v = (innerParts.length > 1) ? innerParts[1] : "";
      res[k]=v;
    }
  }
  return res;
}
function parseJSON(json){ // преобразование в json вид
  if(typeof(json) == 'string'){
	if( json.indexOf('"')==-1 ) json = StrToObj( json );
	json = jQuery.parseJSON( json );
  }else if(json === undefined){
	json = {};
  }else if(typeof(json) != 'object'){
	json = 0;
  }
  return json;
}
function getURLjson( n ){ // gu = getURLjson('')
  if(n=='') n = 'aj';
  var pu = parseURL('');
  pu[n] = parseJSON(pu[n]);
  
  return pu;
}
function putURLjson( put , do_hash ){ // gu = putURLjson('a:3,k:r',1);
  var gu = getURLjson('');
  put = dellElemObj(put,{"ajax":"i","current_currency":"i"}); //
  
  if(do_hash=='ins'){ // ins rep none
	//put = jQuery.grep(put, function(v,i){ return i!="ajax"; },false);
	//delete put.ajax;
	gu['aj'] = AdditionObj( put, gu['aj']);
	
    gu['aj'] = JSONtoROW( gu['aj'], 'ajax' );  //
	
  }else if(do_hash=='rep'){
    gu['aj'] = JSONtoROW(put,'ajax');;
  }
  
  gu = JSONtoROW(gu,'get');
  
  window.location.hash = '?'+gu;
  
  return gu;
}
function JSONtoROW(json,type){ // JSONtoROW(var,'json');
  var row = '';
  jQuery.each(json, function(i,v){
    if(row!='') row += (type=='get')?'&':',';
    row += i;
    row += (type=='get')?'=':':';
    row += (typeof(v)=='object')?JSONtoROW(v,type):v;
  });
  if(type!='get') row = '{'+row+'}';
  return row;
}
function QuesOrEpershand(url){
  return (url.indexOf('?')==-1)?'?':'&';
}
function QuesOrSlash(url){
  return (url.indexOf('=')==-1)?'/':'?';
}
function AdditionObj(obj1,obj2){ // приоритет по индексом у obj1 /// == jQuery.merge
  if (obj1 === undefined) obj1 = {};
  if (obj2 === undefined) obj2 = {};
  jQuery.each(obj1, function(i,k){
    obj2[i]= k;
  });
  return obj2;
}
function dellElemObj(obj,dell){
  var obj = parseJSON(obj);
  var res = {};
  jQuery.each(obj, function(io,vo){
	fp = 0;
	jQuery.each(dell, function(id,vd){
	  if((vd=="i" && io==id) || (vd=="v" && vo==id)){
		fp = 1;
	  }
	});
	if(fp==0){ res[io]=vo; }
  });
  return res;
}

function StrToObj( str ){
  res='';
  var ch = '{}[]:,';
  
  if(str.substr(0,1)!='{') str = '{'+str;
  if(str.substr(str.length-1,1)!='}') str = str+'}';
  
  fl = 0; // 0-close, 1-open
  for(var i=0;i<(str.length);i++){
    var c1 = str.charAt(i-1);
    var c2 = str.charAt(i);
    if( ch.indexOf(c1) >= 0 && ch.indexOf(c2) == -1 && fl == 0){ // открываем
      res += '"'+str.charAt(i);
      fl = 1;
    }else if( ch.indexOf(c2) >= 0 && ch.indexOf(c1) == -1 && fl == 1){ // закрываем
      res += '"'+str.charAt(i);
      fl = 0;
    }else{ 
      res += str.charAt(i);
    }
  }
  if(fl==1) res=0; 
  return res;
}
function count_obj( obj ){
  res=0;
  jQuery.each(obj, function(i,v){
    res++;
  });
  
  return res;
}
function str_replace ( search, replace, subject ) {	// Replace all occurrences of the search string with the replacement string
  if(!(replace instanceof Array)){
	replace=new Array(replace);
	if(search instanceof Array){//If search	is an array and replace	is a string, then this replacement string is used for every value of search
	  while(search.length>replace.length){
		  replace[replace.length]=replace[0];
	  }
	}
  }
  if(!(search instanceof Array))search=new Array(search);
  while(search.length>replace.length){//If replace	has fewer values than search , then an empty string is used for the rest of replacement values
	replace[replace.length]='';
  }
  if(subject instanceof Array){//If subject is an array, then the search and replace is performed with every entry of subject , and the return value is an array as well.
	for(k in subject){
	  subject[k]=str_replace(search,replace,subject[k]);
	}
	return subject;
  }
  for(var k=0; k<search.length; k++){
	var i = subject.indexOf(search[k]);
	while(i>-1){
	  subject = subject.replace(search[k], replace[k]);
	  i = subject.indexOf(search[k],i);
	}
  }

  return subject;
}



