$(document).ready(function(){

// -------------------------------------------------
// CUSTOM PRELOAD FOR users photo and gift images
// -------------------------------------------------
    if ( customImagesPreload === true )
    {
        $('.sframe img, .uframe img, img#userPhoto, img#roomLogo, .im img, #tooltip img.points_img, .screenshot_win img, .screenshot_ipad img, screenshot_iphone img').each(function() {
            if(!this.complete) {
    		    $(this).css({opacity: 0});
            }
    	});

        $('.sframe img, .uframe img, img#userPhoto, img#roomLogo, .im img, #tooltip img.points_img, .screenshot_win img, .screenshot_ipad img, screenshot_iphone img').one('load', function() {
    		$(this).delay(250).animate({opacity:1}, 350);
    	}).each(function() {
            if(this.complete)
    		    $(this).trigger('load');
    	});
    }

    // Notify for IE6 users
    /*if ( IE6Notify == true && $.cookie('ieInfoClose') == 'null')
    {
        var ie6 = (navigator.userAgent.search('MSIE 6.0') != -1);
        if(ie6) {
            var _this = $('#ieInfo');
            var height = _this.outerHeight();
            _this.css('top', '-'+height+'px').show().delay(600).animate({'top': '+='+height+'px'}, 'slow').delay(10800).animate({'top': '-='+height+'px'}, 'slow');

            $('.ieInfoCloseBtn').click(function() {
                d=new Date;
                _this.stop().animate({'top': '-='+height+'px'}, 'slow');
                $.cookie('ieInfoClose', true, {expires: 1}); // create a cookie for 1 day
            });
        }
    }*/

	var feed_cnt = 0;

	$.fn.newFeedCountHelper = function() {
      $.getJSON('/feed/get_new_feed_count.php?callback=?', function(data) {
	  //$.getJSON(BASE_PROFILES_URL+'/feed/get_new_feed_count.php?callback=?', function(data) {
          if ( data.st == 'OK' && data.c != 0 ) {
          	if ( data.c > 20 ) {
          		$("div#feed_cntr").css({"background-image": "url("+ BASE_PROFILES_URL +"/images/feed_cntr_big.png)", "width": "58px"}).show();
          		$('a#cnt').text('More');
          	} else {
          		$("div#feed_cntr").css({"background-image": "url("+ BASE_PROFILES_URL +"/images/feed_cntr.png)", "width": "40px"}).show();
          		$('a#cnt').text(data.c);
          	}
          } else {
          	$('#feed_cntr').hide();
          }
      });
	};

    /*if ( frogFeedCounterAllow === true )
    {
	    $("document").newFeedCountHelper();
        window.setInterval('$("document").newFeedCountHelper()', frogFeedCounterTime);
    }*/
	
	$("input[type!='submit'][type!='checkbox'][type!='radio']").live("focusin", function() {
		$(this).css({'border-color': '#aadf9a', 'background-color': '#f7fff5', 'color': '#000000'});
	}).live("focusout", function() {
		$(this).css({'border-color': '#c1cabf', 'background-color': '#ffffff', 'color': '#898E89'});
	});
	
	$("textarea").live("focusin", function() {
		$(this).css({'border-color': '#aadf9a', 'background-color': '#f7fff5', 'color': '#000000'});
	}).live("focusout", function() {
		$(this).css({'border-color': '#c1cabf', 'background-color': '#ffffff', 'color': '#898E89'});
	});
	
	$("a.button").live("mouseover", function() {
		$(this).children('div').css({'background-position': '0 100%'})
			   .end().children('span').css({'background-position': '100% 100%'});
	}).live("mouseout", function() {
		$(this).children('div').css({'background-position': '0 0'})
			   .end().children('span').css({'background-position': '100% 0'});
	});
	
	$("a.btn_big").live("mouseover", function() {
		$(this).children('div').css({'background-position': '0 100%'})
			   .end().children('span').css({'background-position': '100% 100%'});
	}).live("mouseout", function() {
		$(this).children('div').css({'background-position': '0 0'})
			   .end().children('span').css({'background-position': '100% 0'});
	});
	
	/*$('#statustext').keydown(function(event) {
		if (event.keyCode == '13') {
			$('#statusform').submit();
			return false;
		}
	});*/
	
	$('textarea.comment').live('keydown', function(event) {
		if (event.ctrlKey && event.keyCode == '13') {
			if ($(this).next("#post_com").is(':visible')) {
				$(this).next("#post_com").click();
			}
			return false;
		}
	});
	
	$('.btn_green').live('mouseover',function() {
		$(this).addClass('over');
	}).live('mouseout',function(){                             
		$(this).removeClass('over');    
	});
	
	$('#give_gift_img').live('mouseover',function() {
		$(this).attr('src', BASE_PROFILES_URL +'/images/profiles/give_gift_btn_over.gif');
	}).live('mouseout',function(){                             
		$(this).attr('src', BASE_PROFILES_URL +'/images/profiles/give_gift_btn.gif');       
	});
	
});

function setRealSize() {        
	/*$(document).ready(function(){
		$('body').css('height', '');
		$('html').css('height', '');
		var bodyHeight = $(document).height(); 
		var windowHeight = $.browser.opera? window.innerHeight : $(window).height();
		
		if ( bodyHeight <= windowHeight )
		{
			$('body').css('height', '100%');
			$('html').css('height', '100%'); 
		}
		else
		{
			$('body').css('height', bodyHeight+'px');
			$('html').css('height', bodyHeight+'px'); 
		}
	})*/
}   

function resetRealSize() {  
	$(document).ready(function(){
		var bodyHeight = $(document).height();
		var windowHeight = $.browser.opera? window.innerHeight : $(window).height();
		
		$('body').css('height', '');
		$('html').css('height', '');   
		$('body').css('height', bodyHeight+'px');
		$('html').css('height', bodyHeight+'px'); 
	});
}

function getErrorMessage(code, type) {
    if (!code) {
        code = 0;
    }

    switch (type) {
        case 'profile':
            message = profileErrors[code];
            break;
        case 'friends':
            message = friendsErrors[code];
            break;
        case 'videochat':
            message = videochatErrors[code];
            break;
        default:
            message = profileErrors[code];
            break;
    }

    return "<div class='clearfloat' style='float: none;'></div> <div id='errorBlock' style='margin: 80px 5px 80px 5px; float: none; width: 100%; text-align: center;'><span style='color: #929694; font-size: 14px; font-weight: normal;'>"+message+"</span></div>";
}

// YouTube Video Window
VideoWindow = function() {
    this.show = function(id) {
        this.hide();
        if (id) {
            var html = '<object style="margin-top: 5px;" width="640" height="480"><param name="movie" value="http://www.youtube.com/v/'+ id +'&hl=ru&fs=1&autoplay=0&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'+ id +'&hl=ru&fs=1&autoplay=0&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="480"></embed></object>';

            var container = $('<div></div>')
                .attr({
                	'id':  'VideoWindow'
                });

            var VideoWindowArea = $('<div></div>')
                .attr({
                	'id':  'VideoWindowArea'
                })
                .appendTo(container);

            if (html) {
                VideoWindowArea.html(html);
            }

            container.append('<div class="clearfloat"></div>');

            var centerContainer = $('<center></center>')
                .appendTo(container);

            var VideoWindowArea = $('<div></div>')
                .css({
                    'width': '74px',
                    'margin-top': '10px'
                })
                .append('<a href="javascript:VideoWindow.hide();" class="button"><div id="btnCloseVideoWindow"><p>Close</p></div><span></span></a>')
                .appendTo(centerContainer);

            container.append('<div class="clearfloat"></div>');

            $('body').append(container);

            /*$(document).bind('keydown', function(e) {
                if (e.keyCode == 27) // Event for ESC key
                {
                    VideoWindow.hide();
                    $(document).unbind('keydown');
                }
                return true;
            });*/
			$(document).bind("closeVideoWindow", function(){
			  VideoWindow.hide();
			  $(document).unbind('closeVideoWindow');
			});
			$(document).bind('keydown', function(e) {
				if (e.keyCode == 27) // Event for ESC key
				{
					$(document).trigger("closeVideoWindow");
				}
			});
        }
    }
    this.hide = function() {
        $('#VideoWindow').hide().remove();
    }
}

var VideoWindow = new VideoWindow();
// /YouTube Video Window

// a Function for dymamic loading JS and CSS files
function isArray(obj) { return Object.prototype.toString.call(obj) === '[object Array]'; }

var StaticFiles = {
  '/js/test.js' : {'v':11},
  '/css/ie6.css' : {'v':11}
}

var stManager = {
  wait: [],
  add: function(files,callback) {
    for(var f in files) {
      if ( files[f].v != undefined ) var v = '?v='+files[f].v;
      if (f.indexOf('.js') != -1) {
        var obj = $('<script>')
            .attr({'src':f+v,'type':'text/javascript'})
            .appendTo('head').bind('load', function() {
              if (callback) {
                var i = stManager.wait.push(f);
                stManager.delWait(i);
              }
            });
      } else if (f.indexOf('.css') != -1) {
        var obj = $('<link>')
            .attr({'href':f+v,'rel':'stylesheet','type':'text/css'})
            .appendTo('head');
      }
    }
    if (!callback) return;
    if (!stManager.wait.length) return callback();
    var waitTimer = setTimeout('stManager.waiters('+callback+')', 100);
  },
  waiters: function(callback) {
    if (!stManager.wait.length) {
      clearTimeout(waitTimer);
	  if (!callback) return;
      return callback();
    }
  },
  delWait: function(i) {
    stManager.wait.splice(i,1);
  }
}

//stManager.add(StaticFiles, function() { test() });
