/**
 * JS :: Webusr
 *
 * Webusr.js
 *
 * @version        $1.0$
 * @autor   	   $Peter Benicky$
 */
	 
function Webusr(webroot) {
	webThis = this;
	this.server = webroot;
	this.images = webroot+"css/user/images/";
	this.axajRequest = null;	
	this.initialize();
	this.insertion = null;
}
Webusr.prototype.initialize = function() {
	$("#loading").hide();
	$(".disable_element").hide();
	$(".img_select > li > a").bind("click", webThis.showImg);
	$("a.zoom").bind("click", webThis.zoomIn);
	$("a.add_help").bind("mousemove", webThis.showHelp);
	$("a.add_help").bind("mouseout", function() {$('#map_location').hide();});
	$("a.add_more").bind("click", webThis.showMore);
	$("a.close").bind("click", webThis.close);
	$("div.insertion .disabled").hide();
	$("div.insertion").css({cursor: "pointer"});
	$("div.opened").css({cursor: ""});
	$("div.sold").css({cursor: ""});
	$("div.opened .disabled").show();
	$(".enable").css({display: "inline"});
	$("div.opened .enable").css({display: "none"});
	$("a.add-to-favourites").bind("click", webThis.markInsertion);
	$("a.remove-from-favourites").bind("click", webThis.unMarkInsertion);
	$("a.edit-listings").bind("click", function(){location.href = this.href;});
	$("a.add-extras-listing").bind("click", function(){location.href = this.href;});
	$("a.mark-as-sold").bind("click", webThis.markAsSold);
	$("a.mark-as-available").bind("click", webThis.markAsAvailable);
	$("li.print > a").bind("click", webThis.print);
	$("a.share").bind("click", webThis.showShareForm);
	$("a.link-to-listing").bind("click", webThis.showLinkForm);
	$("#listing_link input").bind("click", function(){this.focus(); this.select();});
	$("li.seller > a").bind("click", webThis.showSellerForm);
	$("div.insertion").bind("click", webThis.showInsertion);
	$("a.letter").bind("click", webThis.loadInsertions);
	$("a.browse_category").bind("click", webThis.loadInsertions);
	$("#paging a").bind("click", webThis.loadInsertions);
	$("map#map_browse > area").bind("click", webThis.loadInsertions);
	$("a.buy_letter").bind("click", webThis.loadMenu);
	$("a.add_category").bind("click", webThis.loadMenu);
	$("#map_add > area").bind("click", webThis.loadMenu);
	$("textarea[@name=text]").keyup(webThis.textCounter);
	$("textarea[@name=text]").keydown(webThis.textCounter);
	$("textarea[@name=text]").focus(webThis.textCounter);
	$("map").bind("mouseout", function() {$('#map_location').hide();});
	$('#map_location').hide();
	$("area").bind("mousemove", webThis.showTitle);
	$("a.add_extras_help").bind("click", webThis.showExtrasHelp);
	$(".img_bar > li > a").bind("click", webThis.switchImg);
	$("input.extra").bind("click", webThis.Count);
	$("input.buyed").bind("click", function() {return false;});
	$("#date").bind("keypress", function() {return false;});
	$("#date_available").bind("keypress", function() {return false;});
	$("a.delete_picture").bind("click", webThis.deletePicture);
	$('a.top_listing').bind("click", webThis.topListing);
	$('a.top-insertion').bind("click", webThis.topInsertion);
	$('li#menu_more > a').bind("click", webThis.showMenuMore);
	$('input#send-seler').bind("click", webThis.contactSeller);
	$('input#send-mail').bind("click", webThis.share);
	$('input[@name=resend]').bind("click", webThis.resend);
	$('input[@name=PayPal]').bind("click", webThis.payPal);
}
Webusr.prototype.payPal = function() {
	var tran_id = webThis.sha1(Math.random()+$('input[@name=hash]').val());
	$('input[@name=return]').val('http://'+location.host+webThis.server+'thank-you/'+tran_id+'/'+$('input[@name=hash]').val()+'/'+$('input[@name=id]').val()+'/');
	webThis.ajaxRequest = $.ajax({
		type: 'POST',
		url: webThis.server+'insertions/set_extras/',
		data: 'tran_id='+tran_id+'&hash='+$('input[@name=hash]').val()+'&amount='+$('input[@name=amount]').val()+'&id='+$('input[@name=id]').val(),
		success: function(msg) {
			if(msg.search('error') != -1) {
				$("div#umsg").empty().append(msg);
   				return false;
   			}
			else
				return true;
		}
   	});
   	if($('input[@name=amount]').val() == '0') {
   		return false;
   	}
} 
Webusr.prototype.print = function() { 
    var link = this;
	webThis.ajaxRequest = $.ajax({
		url: link.href,
		success: function(msg) {
			var iframe=document.createElement('IFRAME'); 
			var doc=null; 
			$(iframe).attr('style','position:absolute;width:0px;height:0px;left:-500px;top:-500px;'); 
			document.body.appendChild(iframe); 
			doc=iframe.contentWindow.document; 
			doc.write(msg);
			doc.close(); 
			iframe.contentWindow.focus(); 
			iframe.contentWindow.print(); 
			wait(1);
			document.body.removeChild(iframe);
			$('#loading').hide();
   		}
   	});
	return false;	
} 
Webusr.prototype.resend = function() {
	$('#loading').show();
	var link = this;
	webThis.ajaxRequest = $.ajax({
		type: 'POST',
		url: webThis.server+'resend-editing-link/',
		data: 'ajax=true&email='+$("input[@name=email]").val(),
		success: function(msg) {
			if (msg.search('tick') != -1) {
				$("input[@name=email]").val('');
			}
			$("div#umsg").empty().append(msg);
   			$('#loading').hide();
		}
   	});
	return false;
}
Webusr.prototype.contactSeller = function() {
	$('#loading').show();
	var link = this;
	webThis.ajaxRequest = $.ajax({
		type: 'POST',
		url: webThis.server+'contact-the-seller/',
		data: 'ajax=true&captcha='+$("input[@name=captcha]").val()+'&to_c='+$("input[@name=to_c]").val()+'&email_c='+$("input[@name=email_c]").val()+'&name_c='+$("input[@name=name_c]").val()+'&body_c='+$("textarea[@name=body_c]").val(),
		success: function(msg) {
			if (msg.search('tick') != -1) {
				webThis.close();
				$("input[@name=email_c]").val('');
				$("input[@name=name_c]").val('');
				$("textarea[@name=body_c]").val('');
				$("input[@name=captcha]").val('');
			}
			else {
				webThis.ajaxRequest = $.ajax({
				url: webThis.server+'insertions/captcha/'+Math.random()+'/',
				success: function(captcha) {
					$('span.captcha').empty().append(captcha);
					$("input[@name=captcha]").val('');
		   		}
				});
			}
			$("p.error").remove();
			$('div#insertion-'+$("input[@name=to_c]").val()).after(msg);
   			$('#loading').hide();
   		}
   	});
	return false;
}
Webusr.prototype.share = function() {
	$('#loading').show();
	var link = this;
	webThis.ajaxRequest = $.ajax({
		type: 'POST',
		url: webThis.server+'share/',
		data: 'ajax=true&to_s='+$("input[@name=to_s]").val()+'&email_s='+$("input[@name=email_s]").val()+'&name_s='+$("input[@name=name_s]").val()+'&your_email_s='+$("input[@name=your_email_s]").val(),
		success: function(msg) {
			if (msg.search('tick') != -1) {
				webThis.close();
				$("input[@name=email_s]").val('');
				$("input[@name=name_s]").val('');
				$("input[@name=your_email_s]").val('');
			}
			$("p.error").remove();
			$('div#insertion-'+$("input[@name=to_s]").val()).after(msg);
   			$('#loading').hide();
		}
   	});
	return false;
}
Webusr.prototype.showMenuMore = function() {
	if($('li#menu_more ul').attr('style'))
	{
		 $('li#menu_more ul').hide();
		 $('li#menu_more ul').attr('style', '');
		 return false;
	}
	webThis.close();
    $('li#menu_more ul').css({position: "absolute", top: ""+($(this).position()['top'] - ($('li#menu_more ul').height() + 15))+"px", left: ""+$(this).position()['left']+"px" }).show();
	return false;	
}
Webusr.prototype.topListing = function() {
	$('#loading').show();
	webThis.close();
	var link = this;
	webThis.ajaxRequest = $.ajax({
		url: link.href+Math.random()+'/',
		success: function(msg){
			$('div#top_dogs_gallery').empty();
			$('div#top_dogs_gallery').append(msg);
    		$('#loading').hide();
   		}
	});
	return false;	
}
Webusr.prototype.topInsertion = function() {
	$('#loading').show();
	webThis.close();
	var link = this;
	webThis.ajaxRequest = $.ajax({
		type: 'POST',
		url: link.href,
		data: 'ajax=true',
		success: function(msg) {
			webThis.close();
			$('div#insertion-'+link.rel).after(msg);
   			$('#loading').hide();
   		}
   	});
	return false;
}
Webusr.prototype.markAsSold = function() {
	$('#loading').show();
	webThis.close();
	var link = this;
	webThis.ajaxRequest = $.ajax({
		type: 'POST',
		url: link.href,
		data: 'ajax=true',
		success: function(msg) {
			webThis.close();
			$('div#insertion-'+link.rel).after(msg);
			$('div#insertion-'+link.rel).removeClass('border');
			$('div#insertion-'+link.rel).addClass('sold');
			$("div#insertion-"+link.rel+" .disabled").hide();
			$("div#insertion-"+link.rel+" .enable").show();
			$("div#insertion-"+link.rel+"").css({cursor: "pointer"});
			$("div#insertion-"+link.rel+" div.text").css({cursor: "pointer"});
			$("div#insertion-"+link.rel+" span.sold > img").show();
			$("div#insertion-"+link.rel+" ul.options li").hide();
			$("div#insertion-"+link.rel+" li.mark_as_available").show();
   			$('#loading').hide();
   		}
   	});
	return false;
}
Webusr.prototype.markAsAvailable = function() {
	$('#loading').show();
	webThis.close();
	var link = this;
	webThis.ajaxRequest = $.ajax({
		type: 'POST',
		url: link.href,
		data: 'ajax=true',
		success: function(msg) {
			webThis.close();
			$('div#insertion-'+link.rel).removeClass('sold');
			$("div#insertion-" + link.rel + " span.sold > img").hide();
			if(msg.search('border') != -1)
				$('div#insertion-'+link.rel).addClass('border');	
			$('div#insertion-'+link.rel).after(msg);
   			$('#loading').hide();
			$("div#insertion-"+link.rel+" ul.options li").show();
			$("div#insertion-"+link.rel+" li.mark_as_available").hide();
   		}
   	});
	return false;
}
Webusr.prototype.deletePicture = function() {
	$('#loading').show();
	webThis.close();
	var link = this;
	webThis.ajaxRequest = $.ajax({
		url: link.href+Math.random()+'/',
		success: function(msg) {
			$(link).fadeOut();
			$('#loading').hide();
	   	}
	});
	return false;
}
Webusr.prototype.Count = function(e){
	var count = parseInt($('span#total').text());
	var value = parseInt($(this).attr('value'));
	
	if($(this).attr('checked') == true)
		count = count + value;
	else 
		count = count - value;
	if(count == 0)
		count = '0';
	$('span#total').html(count);
	$("input[@name=amount]").val(count);
}
Webusr.prototype.showTitle = function(e){
	 var cssObj = {
        left: (e.pageX + 10)+'px',
        top: (e.pageY + 10)+'px'
    }
	$('#map_location').css(cssObj);
	$('#map_location').html(this.alt).show();
}
Webusr.prototype.loadMenu = function() {
	$('#loading').show();
	webThis.close();
	if(($(this).attr('class')) == 'add_category' || ($(this).attr('class')) == 'buy_letter actived' || ($(this).attr('class')) == 'buy_letter' || ($(this).attr('class')) == 'actived add_category')
		var link = this.href+Math.random()+'/';
	else
		var link = $('#map_link').attr('href')+this.id+'/all-counties/'+Math.random()+'/';
	webThis.ajaxRequest = $.ajax({
		url: link,
		success: function(msg) {
			$('div#add_menu').empty().append(msg);
			$('#loading').hide();
			if(($('#map_link').attr('rel')) == 'uk' || (link.search('/uk/all-counties')) != -1)
				$('#counties').show();
			else
				$('#counties').hide();	
			window.scroll(0,0);
	   	}
	});
	return false;
}
Webusr.prototype.textCounter = function() {
	if($("textarea[@name=text]").val().length > 600)
		$("#counter").empty().append(' <span class="red"> 0 </span>');
	else
		$("#counter").empty().append(''+(600 - $("textarea[@name=text]").val().length)+'');
}
Webusr.prototype.showInsertion = function() {
	webThis.close();
	if(($(this).attr("class").search("opened")) != -1)
		return false;
	if(($(this).attr("class").search("sold")) != -1)
		return false;
	if(webThis.insertion != null) {
		$("#"+webThis.insertion.id+" .disabled").hide();
		$("#"+webThis.insertion.id+" .enable").show();
		$("#"+webThis.insertion.id+"").css({cursor: "pointer"});
		$("#"+webThis.insertion.id+" div.text").css({cursor: "pointer"});
	}
	webThis.insertion = this;
	$("#"+this.id+" ul.disabled").show();
	$("#"+this.id+" div.disabled").show();
	$("#"+this.id+" span.disabled").css({display: "inline"});
	$("#"+this.id+"").css({cursor: "default"});
	$("#"+this.id+" div.text").css({cursor: "default"});
	$("#"+this.id+" .enable").hide();
	return false;	
}
Webusr.prototype.showSoldInsertion = function() {
	webThis.close();
	if(($(this).attr("class").search("opened")) != -1)
		return false;
	if(webThis.insertion != null) {
		$("#"+webThis.insertion.id+" .disabled").hide();
		$("#"+webThis.insertion.id+" .enable").show();
		$("#"+webThis.insertion.id+"").css({cursor: "pointer"});
		$("#"+webThis.insertion.id+" div.text").css({cursor: "pointer"});
	}
	webThis.insertion = this;
	$("#"+this.id+" ul.disabled").show();
	$("#"+this.id+" div.disabled").show();
	$("#"+this.id+" span.disabled").css({display: "inline"});
	$("#"+this.id+"").css({cursor: "default"});
	$("#"+this.id+" div.text").css({cursor: "default"});
	$("#"+this.id+" .enable").hide();
	return false;	
}
Webusr.prototype.showImg = function() {
	webThis.close();
	var uniq = this.rel;
	var id = uniq.substring(0,(uniq.length - 1));
	$('a.img_show-'+id).removeClass('actived');
	$(this).addClass('actived');
	$('img.thumb-'+id).addClass('disable');
	$('img#img-'+uniq).removeClass('disable');
	return false;
}
Webusr.prototype.loadInsertions = function() {
	$('#loading').show();
	webThis.close();
	if(($(this).attr('class')) == 'browse_category' || ($(this).attr('class')) == 'letter actived'  || ($(this).attr('class')) == 'letter' || ($(this).attr('class')) == 'actived browse_category' || ($(this).attr('class')) == 'paging_link' || ($(this).attr('class')) == 'paging_link actived')
		var link = this.href+Math.random()+'/';
	else
		var link = $('#map_link').attr('href')+this.id+'/all-counties/'+$('#map_link').attr('rev')+'/'+Math.random()+'/';
	webThis.ajaxRequest = $.ajax({
		url: link,
		success: function(msg) {
			$('div#layout').empty().append(msg);
			$('#loading').hide();
			if(($('#map_link').attr('rel')) == 'uk')
				$('#counties').show();
			else
				$('#counties').hide();	
	   		window.scroll(0,0);
			$('#loading').hide();
		}
	});
	return false;
}
Webusr.prototype.showShareForm = function() {
    webThis.close();
    $("input[@name=to_s]").val(this.rel);
    $('form#send').css({margin: 0, position: "absolute", top: ""+($('#insertion-' + this.rel).position()['top'] + 3)+"px", left: ""+($('#insertion-' + this.rel).position()['left'] + 545)+"px"  });
    $('form#send').show();
	return false;
}
Webusr.prototype.showLinkForm = function() {
    webThis.close();
    $('form#listing_link').css({margin: 0, position: "absolute", top: ""+($('#insertion-' + this.rel).position()['top'] + 3)+"px", left: ""+($('#insertion-' + this.rel).position()['left'] + 545)+"px"  });
    $("input[@name=html_link]").val('<a href="'+this.href+'" title="'+this.title+'">'+this.title+'</a>');
	$("input[@name=http_link]").val(this.href);
	$('form#listing_link').show();
	return false;
}
Webusr.prototype.showHelp = function(e) {
	var cssObj = {
        left: (e.pageX + 10)+'px',
        top: (e.pageY + 10)+'px'
    }
	$('#map_location').css(cssObj);
	$('#map_location').html(this.rel).show();
	return false;
}
Webusr.prototype.showMore = function() {
	$('li#menu_more ul').hide();
	$('li#menu_more ul').attr('style', '');
	$(".show_more").hide();
	$(".add_picture").show();
	return false;
}
Webusr.prototype.showSellerForm = function() {  
	webThis.ajaxRequest = $.ajax({
		url: webThis.server+'insertions/captcha/'+Math.random()+'/',
		success: function(captcha) {
			$('span.captcha').empty().append(captcha);
			$("input[@name=captcha]").val('');
		}
	});
	webThis.close();
    $("input[@name=to_c]").val(this.rel);
    $('form#contact_seller').css({margin: 0, position: "absolute", top: ""+($('#insertion-' + this.rel).position()['top'] + 3)+"px", left: ""+($('#insertion-' + this.rel).position()['left'] + 545)+"px"  });
    $('form#contact_seller > h3').html(this.title);
	$('form#contact_seller').show();
	return false;
}
Webusr.prototype.showExtrasHelp = function() {
	$('#zoom_window > img').attr('src', '');
	$('#zoom_window > img').hide();
	var img = this.href;
	var picture = new Image();
	var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
	var size = this.rev;
	var posTop, posLeft, picWidth, picHeight;
	picWidth = parseInt(size.substring(0, size.indexOf('-') + 1));
	picHeight = parseInt(size.substring(size.indexOf('-') + 1));
	availHeight = screen.height - 200;
    availWidth = screen.width;

	if (availHeight <= picHeight) {posTop = 0;}
    else {posTop = (availHeight - picHeight)/2;}
	if (availWidth <= picWidth) {posLeft = 0;}
    else {posLeft = (availWidth - picWidth)/2;}

    var cssObj = {
        left: posLeft+'px',
        top: posTop+'px',
		width: (picWidth + 20)+'px',
		height: (picHeight + 74)+'px'
    }
	if(IE6) 
	{
		var cssObj = {
			"margin-left": '-45px', 
			position: 'absolute',
			width: (picWidth + 20)+'px',
			height: (picHeight + 74)+'px'
    	}	
	}
	
	webThis.close();
	$('#zoom_window').css(cssObj);
	$('#zoom_window > img').attr('alt', this.title);
	$('#zoom_window > h3').html(this.title);
	$('#zoom_window').show();
	picture.src = img;
  if(picture.complete) {
    $('#zoom_window > img').attr('src', img);
    $('#zoom_window > img').show();
    picture = null;
  }
  else {
    $(picture).load(function() {
  		$('#zoom_window > img').attr('src', img);
  		$('#zoom_window > img').show();
  		picture = null;
  	});
  }
	return false;	
	return false;	
}
Webusr.prototype.zoomIn = function() {
	$('#zoom_window > img').attr('src', '');
	$('#zoom_window > img').hide();
  var id = this.rel;
	var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
	var img = $('.img_select > li > a.actived.img_show-'+id).attr('href');
	var size = $('.img_select > li > a.actived.img_show-'+id).attr('rev');
  var picture = new Image();
	var posTop, posLeft, picWidth, picHeight;
	picWidth = parseInt(size.substring(0, size.indexOf('-') + 1));
	picHeight = parseInt(size.substring(size.indexOf('-') + 1));
	availHeight = screen.height - 200;
  availWidth = screen.width;

	if (availHeight <= picHeight) {posTop = 0;}
    else {posTop = (availHeight - picHeight)/2;}
	if (availWidth <= picWidth) {posLeft = 0;}
    else {posLeft = (availWidth - picWidth)/2;}

    var cssObj = {
        left: posLeft+'px',
        top: posTop+'px',
		width: (picWidth + 20)+'px',
		height: (picHeight + 74)+'px'
    }
	if(IE6) 
	{
		var cssObj = {
			"margin-left": (480 -  picWidth)+'px', 
			position: 'absolute',
			width: (picWidth + 20)+'px',
			height: (picHeight + 74)+'px'
    	}	
	}
	
	$('#zoom_window').css(cssObj);
	$('#zoom_window > img').attr('src', img);
	$('#zoom_window > img').attr('alt', this.title);
	$('#zoom_window > h3').html(this.title);
	webThis.close();
	$('#zoom_window').show();
	
	$('#zoom_window .photo_1').removeClass("disable");	
	$('#zoom_window .photo_2').removeClass("disable");	
	$('#zoom_window .photo_3').removeClass("disable");	
	$('#zoom_window .photo_4').removeClass("disable");	

	$('#zoom_window .photo_1').html($('.img_select > li > a.photo_1.img_show-'+id).text());
	$('#zoom_window .photo_1').attr('href', $('.img_select > li > a.photo_1.img_show-'+id).attr('href'));
	$('#zoom_window .photo_1').attr('rev', $('.img_select > li > a.photo_1.img_show-'+id).attr('rev'));
	$('#zoom_window .photo_1').attr('rel', $('.img_select > li > a.photo_1.img_show-'+id).attr('rel'));
	$('#zoom_window .photo_1').attr('title', $('.img_select > li > a.photo_1.img_show-'+id).attr('title'));
	$('#zoom_window .photo_1').attr('class', $('.img_select > li > a.photo_1.img_show-'+id).attr('class'));

	$('#zoom_window .photo_2').html($('.img_select > li > a.photo_2.img_show-'+id).text());
	$('#zoom_window .photo_2').attr('href', $('.img_select > li > a.photo_2.img_show-'+id).attr('href'));
	$('#zoom_window .photo_2').attr('rev', $('.img_select > li > a.photo_2.img_show-'+id).attr('rev'));
	$('#zoom_window .photo_2').attr('rel', $('.img_select > li > a.photo_2.img_show-'+id).attr('rel'));
	$('#zoom_window .photo_2').attr('title', $('.img_select > li > a.photo_2.img_show-'+id).attr('title'));
	$('#zoom_window .photo_2').attr('class', $('.img_select > li > a.photo_2.img_show-'+id).attr('class'));
	
	$('#zoom_window .photo_3').html($('.img_select > li > a.photo_3.img_show-'+id).text());
	$('#zoom_window .photo_3').attr('href', $('.img_select > li > a.photo_3.img_show-'+id).attr('href'));
	$('#zoom_window .photo_3').attr('rev', $('.img_select > li > a.photo_3.img_show-'+id).attr('rev'));
	$('#zoom_window .photo_3').attr('rel', $('.img_select > li > a.photo_3.img_show-'+id).attr('rel'));
	$('#zoom_window .photo_3').attr('title', $('.img_select > li > a.photo_3.img_show-'+id).attr('title'));
	$('#zoom_window .photo_3').attr('class', $('.img_select > li > a.photo_3.img_show-'+id).attr('class'));
	
	$('#zoom_window .photo_4').html($('.img_select > li > a.photo_4.img_show-'+id).text());
	$('#zoom_window .photo_4').attr('href', $('.img_select > li > a.photo_4.img_show-'+id).attr('href'));
	$('#zoom_window .photo_4').attr('rev', $('.img_select > li > a.photo_4.img_show-'+id).attr('rev'));
	$('#zoom_window .photo_4').attr('rel', $('.img_select > li > a.photo_4.img_show-'+id).attr('rel'));
	$('#zoom_window .photo_4').attr('title', $('.img_select > li > a.photo_4.img_show-'+id).attr('title'));
	$('#zoom_window .photo_4').attr('class', $('.img_select > li > a.photo_4.img_show-'+id).attr('class'));
	
	if(!($('.img_select > li > a.photo_1.img_show-'+id).attr('href')))
		$('#zoom_window .photo_1').addClass("disable");
	if(!($('.img_select > li > a.photo_2.img_show-'+id).attr('href')))
		$('#zoom_window .photo_2').addClass("disable");
	if(!($('.img_select > li > a.photo_3.img_show-'+id).attr('href')))
		$('#zoom_window .photo_3').addClass("disable");
	if(!($('.img_select > li > a.photo_4.img_show-'+id).attr('href')))
		$('#zoom_window .photo_4').addClass("disable");	
	picture.src = img;
	if(picture.complete) {
		$('#zoom_window > img').attr('src', img);
	    $('#zoom_window > img').show();
	    picture = null;
	}
	 else {
	    $(picture).load(function() {
	  		$('#zoom_window > img').attr('src', img);
	  		$('#zoom_window > img').show();
	  		picture = null;
	  	});
	}
	return false;	
}
Webusr.prototype.switchImg = function() {
  $('#zoom_window > img').attr('src', '');
	$('#zoom_window > img').hide();
	
	var id = this.rel;
	var img = this.href;
  var	picture = new Image();
	var size = this.rev;
	var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
	var posTop, posLeft, picWidth, picHeight;
	picWidth = parseInt(size.substring(0, size.indexOf('-') + 1));
	picHeight = parseInt(size.substring(size.indexOf('-') + 1));
	availHeight = screen.height - 170;
  availWidth = screen.width;

	if (availHeight <= picHeight) {posTop = 0;}
    else {posTop = (availHeight - picHeight)/2;}
	if (availWidth <= picWidth) {posLeft = 0;}
    else {posLeft = (availWidth - picWidth)/2;}

    var cssObj = {
        left: posLeft+'px',
        top: posTop+'px',
		width: (picWidth + 20)+'px',
		height: (picHeight + 74)+'px'
    }
	if(IE6) 
	{
		var cssObj = {
			"margin-left": (480 -  picWidth)+'px', 
			position: 'absolute',
			width: (picWidth + 20)+'px',
			height: (picHeight + 74)+'px'
    	}	
	}
	webThis.close();
	$('#zoom_window').css(cssObj);
	$('#zoom_window > img').attr('alt', this.title);
	$('#zoom_window > h3').html(this.title);
	$('.img_bar > li > a').removeClass('actived');
	$(this).addClass('actived');
	$('#zoom_window').show();
	picture.src = img;
  if(picture.complete) {
    $('#zoom_window > img').attr('src', img);
    $('#zoom_window > img').show();
    picture = null;
  }
  else {
    $(picture).load(function() {
  		$('#zoom_window > img').attr('src', img);
  		$('#zoom_window > img').show();
  		picture = null;
  	});
  }
	return false;	
}
Webusr.prototype.close = function() {
	$('#help_window').hide();
	$('#contact_seller').hide();
	$('form#send').hide();
	$('form#listing_link').hide();
	$('#zoom_window').hide();
	$('p.info').remove();
	$('p.tick').remove();
	$('p.error').remove();
	$('#zoom_window > img').attr('src', "");
	$('li#menu_more ul').hide();
	$('li#menu_more ul').attr('style', '');
	return false;	
}
Webusr.prototype.markInsertion = function() {
	$('#loading').show();
	var link = this;
	var count = parseInt($('span#marked_count').text());
	webThis.ajaxRequest = $.ajax({
		type: 'POST',
		url: link.href,
		data: 'ajax=true',
		success: function(msg) {
			webThis.close();
			$('div#insertion-'+link.rel).after(msg);
			if(msg.search('tick') != -1) {
				$('span#marked_count').empty().append(count + 1);
   			}
   			$('#loading').hide();
   		}
   	});
	return false;
}
Webusr.prototype.unMarkInsertion = function() {
	$('#loading').show();
	var link = this;
	var count = parseInt($('span#marked_count').text());
	webThis.ajaxRequest = $.ajax({
		type: 'POST',
		url: link.href,
		data: 'ajax=true',
		success: function(msg) {
			webThis.close();
			$('div#insertion-'+link.rel).after(msg);
			$('div#insertion-'+link.rel).remove();
			if(count != 1)
				$('span#marked_count').empty().append(count - 1);
			else
				$('span#marked_count').empty().append("0");
			$('#loading').hide();
		}
	});
	return false;
}
Webusr.prototype.sha1 = function(msg) {

	function rotate_left(n,s) {
		var t4 = ( n<<s ) | (n>>>(32-s));
		return t4;
	};

	function lsb_hex(val) {
		var str="";
		var i;
		var vh;
		var vl;

		for( i=0; i<=6; i+=2 ) {
			vh = (val>>>(i*4+4))&0x0f;
			vl = (val>>>(i*4))&0x0f;
			str += vh.toString(16) + vl.toString(16);
		}
		return str;
	};

	function cvt_hex(val) {
		var str="";
		var i;
		var v;

		for( i=7; i>=0; i-- ) {
			v = (val>>>(i*4))&0x0f;
			str += v.toString(16);
		}
		return str;
	};


	function Utf8Encode(string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";

		for (var n = 0; n < string.length; n++) {

			var c = string.charCodeAt(n);

			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}

		}

		return utftext;
	};

	var blockstart;
	var i, j;
	var W = new Array(80);
	var H0 = 0x67452301;
	var H1 = 0xEFCDAB89;
	var H2 = 0x98BADCFE;
	var H3 = 0x10325476;
	var H4 = 0xC3D2E1F0;
	var A, B, C, D, E;
	var temp;

	msg = Utf8Encode(msg);

	var msg_len = msg.length;

	var word_array = new Array();
	for( i=0; i<msg_len-3; i+=4 ) {
		j = msg.charCodeAt(i)<<24 | msg.charCodeAt(i+1)<<16 |
		msg.charCodeAt(i+2)<<8 | msg.charCodeAt(i+3);
		word_array.push( j );
	}

	switch( msg_len % 4 ) {
		case 0:
			i = 0x080000000;
		break;
		case 1:
			i = msg.charCodeAt(msg_len-1)<<24 | 0x0800000;
		break;

		case 2:
			i = msg.charCodeAt(msg_len-2)<<24 | msg.charCodeAt(msg_len-1)<<16 | 0x08000;
		break;

		case 3:
			i = msg.charCodeAt(msg_len-3)<<24 | msg.charCodeAt(msg_len-2)<<16 | msg.charCodeAt(msg_len-1)<<8	| 0x80;
		break;
	}

	word_array.push( i );

	while( (word_array.length % 16) != 14 ) word_array.push( 0 );

	word_array.push( msg_len>>>29 );
	word_array.push( (msg_len<<3)&0x0ffffffff );


	for ( blockstart=0; blockstart<word_array.length; blockstart+=16 ) {

		for( i=0; i<16; i++ ) W[i] = word_array[blockstart+i];
		for( i=16; i<=79; i++ ) W[i] = rotate_left(W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16], 1);

		A = H0;
		B = H1;
		C = H2;
		D = H3;
		E = H4;

		for( i= 0; i<=19; i++ ) {
			temp = (rotate_left(A,5) + ((B&C) | (~B&D)) + E + W[i] + 0x5A827999) & 0x0ffffffff;
			E = D;
			D = C;
			C = rotate_left(B,30);
			B = A;
			A = temp;
		}

		for( i=20; i<=39; i++ ) {
			temp = (rotate_left(A,5) + (B ^ C ^ D) + E + W[i] + 0x6ED9EBA1) & 0x0ffffffff;
			E = D;
			D = C;
			C = rotate_left(B,30);
			B = A;
			A = temp;
		}

		for( i=40; i<=59; i++ ) {
			temp = (rotate_left(A,5) + ((B&C) | (B&D) | (C&D)) + E + W[i] + 0x8F1BBCDC) & 0x0ffffffff;
			E = D;
			D = C;
			C = rotate_left(B,30);
			B = A;
			A = temp;
		}

		for( i=60; i<=79; i++ ) {
			temp = (rotate_left(A,5) + (B ^ C ^ D) + E + W[i] + 0xCA62C1D6) & 0x0ffffffff;
			E = D;
			D = C;
			C = rotate_left(B,30);
			B = A;
			A = temp;
		}

		H0 = (H0 + A) & 0x0ffffffff;
		H1 = (H1 + B) & 0x0ffffffff;
		H2 = (H2 + C) & 0x0ffffffff;
		H3 = (H3 + D) & 0x0ffffffff;
		H4 = (H4 + E) & 0x0ffffffff;

	}

	var temp = cvt_hex(H0) + cvt_hex(H1) + cvt_hex(H2) + cvt_hex(H3) + cvt_hex(H4);

	return temp.toLowerCase();
}
