var windowWidth;

if (self.innerHeight) {    // all except Explorer
    windowWidth = self.innerWidth;
} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
    windowWidth = document.documentElement.clientWidth;
} else if (document.body) { // other Explorers
    windowWidth = document.body.clientWidth;
}
self.moveTo(0,0);
self.resizeTo(screen.availWidth,screen.availHeight);

jQuery.noConflict();
(function($){
    $(function(){
        // resolution
        var resol = 'low';
        var defaultCams = 24;
        var previousResol = getCookie('resolution') != '' ? getCookie('resolution') : resol;
        if (screen.width >= 1280 ) {
          resol = 'high';
          defaultCams = 30;
        }
        if (resol != previousResol) {
          setCookie('cams_perpage', defaultCams, 30);
          setCookie('resolution',resol,30);
          location.reload();
        }

        // reset button in the search form
        $('#zoek-form .reset').click(function(){
            var form = $('#zoek-form');
            form.find('input:checkbox').removeAttr('checked');
            form.find('input[name="search[nickname]"]').val('');
            form.find('input[name="search_category[female]"]').attr('checked','checked');
            form.find('input[name="search_category[couple]"]').attr('checked','checked');
            form.find('select').val('all');
            setCookie('search-options','',0);
            $('#zoek-form').submit();
        });
        // click to collapse or fold search options
        $('#zoek-form ul.buttons .options').click(function(){
            var btn = $(this);
            if ( btn.hasClass('back') )
            {
                $('#zoek-form .reset').fadeOut('slow');
                $('#zoek-form .second-row').slideUp('slow');
                $('input[name="view-ext"]').val(0);
                setCookie('view-ext',0, 7);
                btn.toggleClass('back');
            }
            else
            {
                $('#zoek-form .second-row').slideDown('slow');
                $('#zoek-form .reset').fadeIn('slow');
                $('input[name="view-ext"]').val(1);
                setCookie('view-ext',1, 7);
                btn.toggleClass('back');
            }
        })

        if ( parseInt(getCookie('view-ext')) ){
            $('#zoek-form ul.buttons .options').addClass('back');
            $('#zoek-form .second-row').show();
            $('#zoek-form .reset').fadeIn('slow');
        }

        if ( parseInt($('input[name="view-ext"]').val()) )
        {
            $('#zoek-form .second-row').show();
            $('#zoek-form ul.buttons .options').addClass('back');
        }

        $('#zoek-form input[type="checkbox"]').click(function(){
            var form = $('#zoek-form');
            setCookie('search-options',form.serialize(),7);
            form.submit();
            return false;
        });

        $('#zoek-form select').change(function(){
            var form = $('#zoek-form');
            setCookie('search-options',form.serialize(),7);
            $('#zoek-form').submit();
            return false;
        });

        $('#resultform input[type="checkbox"]').click(function(){
            var form = $('#resultform');
            form.submit();
            return false;
        });

        $('.del-lastvisit a').click(function(){setCookie('lastvisited','',0);});

        // event on search nickname change
        $('input[name="search[nickname]"]').change(function(){
            setCookie('search_nickname', $(this).val(), 7);
        });

        // play clips
        var tm = null;
        $('.clip').hover(
            function(){
              var img = $(this).find('img');
              tm = setInterval(function(){
                 var n = /_(.{2})\.[^.]+$/g.exec(img.attr('src'));
                 if (typeof n == 'object') {
                    n = parseInt(n[1],10);
                    n = n >= 10 ? 1 : ++n;
                    n = n == 10 ? '10' : ('0' + n);
                    img.attr('src', img.attr('src').replace(/_(.{2})\.([^.]+)$/, '_'+n+'.$2'));
                 }
                },600);
            },
            function(){
                !tm || clearInterval(tm);
            }
        );

        $('.tcat').click(function(){
            var self = $(this);
            var cat = self.siblings('.cat');
            if (cat.is(':visible')) {
              cat.stop().slideUp(200);
              self.find('span').removeClass('up');
            } else {
              cat.stop().slideDown(200);
              self.find('span').addClass('up');
            }
        });

    })

    $.showOverlay = function( color, zindex ){
        zindex = zindex == undefined ? 1500 : zindex;
        $('#ovr-stels').remove();
        $('<div/>', {'id':'ovr-stels', 'css':{'position':'fixed','width':'100%','height':'100%','backgroundColor':color,'opacity':0.9,'zIndex':zindex, 'display':'none', 'left' :0, 'top':0}}).appendTo('body').show();
    }

    $.hideOverlay = function(){
        $('#ovr-stels').fadeOut(200, function(){$(this).remove()});
    }
    
})(jQuery)

/* adult warning */
function adultWarning(){
    var warn = getCookie('warn');
    var dlg = jQuery('.warn')
    if (!warn) {
        jQuery.showOverlay('#000', 900);
        dlg.find('.in').click(function(){
             setCookie('warn', 1, 30);
             dlg.slideUp(300, function(){jQuery.hideOverlay();});
             return false;
        });
        dlg.show();
    }
}

/* cameras updating */
var tm = null;
function restartUpdate(){
    stopUpdate();
    tm = setInterval( function(){xajax_filter('page', page)}, 36000);
}

/* stop update cameras */
function stopUpdate(){
  if(tm)
     clearInterval(tm);
}

function delElement(elementId)
{
  element = document.getElementById(elementId);
  if (element) {
    element.parentNode.removeChild(element);
  }
}

function element(id){
	return document.getElementById ? document.getElementById(id) : document.all[id];
}

function refreshCams() {
	xajax_refreshcams();
	setTimeout('refreshcams()', 60000);
}

function maxWindow(url, name, width, height) {
  features = 'scrollbars=1,resizable=1,menubar=0,toolbar=0,location=0,status=0,top=0,left=0,screenX=0,screenY=0,';

  if (window.screen) {
    aw = screen.availWidth;
    ah = screen.availHeight;
    features = features+'width='+(aw-20)+',height='+(ah-38);
  }
  else {
    features = features+'width='+width+',height='+height;
  }
  window.open(url, name, features);
}

function popupWindow(url, w, h) {
  var middleLeft = 400 - w;
  var middleTop = 10;

  if (window.screen) {
    middleLeft = (screen.availWidth / 2) - (w / 2);
  }

  window.open(url, '', 'scrollbars=0,resizable=0,menubar=0,toolbar=0,location=0,status=0,top='+middleTop+',left='+middleLeft+',width='+w+',height='+h);
}

function openNewWindow(URLtoOpen, windowName, windowFeatures) {
  newWindow=window.open(URLtoOpen, windowName, windowFeatures);
}

function addBookmark(title,url) {
	if (window.sidebar) {
		window.sidebar.addPanel(title, url,"");
	} else if( document.all ) {
		window.external.AddFavorite( url, title);
	} else if( window.opera && window.print ) {
	return true;
	}
}

function checkItem(id)
{
	if(element(id).className == 'actlink')
	{

		element(id).className = 'unactlink';

		//element(id).style.fontWeight = 'normal';
		//element(id).style.color = '#000000';
	}
	else
	{
		element(id).className = 'actlink';

		//element(id).style.fontWeight = 'bold';
		//element(id).style.color = '#0ea8d4';
	}

	xajax_filter(id, '1');
}

var img_cache = new Object();

function parse_images() {
	if (document.getElementById) {
		var ar = document.getElementsByTagName('img');
		for( var x = 0; ar[x]; x++ ) {
			var im = ar[x];
			if(im.getAttribute) {
				im.hoversrc = im.getAttribute('hoversrc');
			}
			if(im.hoversrc) {
				im.rootsrc = im.src;
				im.onmouseout = function () {
					this.className = '';
					this.src = this.rootsrc;
				}
				im.onmouseover = function () {
					this.className = this.hoversrc;
				}
			}
		}
	}
}

function OpenBlock() {
	document.getElementById('fo').style.display = 'block';
	document.getElementById('fc').style.display = 'none';
}
function CloseBlock() {
	document.getElementById('fo').style.display = 'none';
	document.getElementById('fc').style.display = 'block';
}

/**
*   Video preview
**/
function startPlayer( videoSrc, image, width, height, skin )
{
    // Config
    var root_player_path = "";
    var defaultWidth  = 520;
    var defaultHeight = 360;
    var defaultSkin   = 'snel.swf';

    // Init
    width  = typeof(width)=='undefined' ? defaultWidth : width;
    height = typeof(height)=='undefined' ? defaultHeight : height;
    skin =   typeof(skin)=='undefined' ? defaultSkin : skin;
    image =  typeof(image)=='undefined' ? '' : image;

    swfobject.embedSWF(
        root_player_path + 'player.swf',
        'videoplayer',
        width,
        height,
        '9.0.0',
        null,
        {
            // Flashvars
            skin: root_player_path + skin,
            file: videoSrc,
            image: image,
            autostart: 'false',
            duration: -1
        },
        {
            //Params
            allowfullscreen: 'true',
            allowscriptaccess: 'always'
        },
        {
            id:'player',
            name:'player'
        }
    );
}

/**
* Cookies
**/
var _cookies;
function _initCookies() {
  _cookies = {};
  var ca = document.cookie.split(';');
  var re = /^[\s]*([^\s]+?)$/i;
  for(var i = 0; i < ca.length; i++) {
    var c = ca[i].split("=");
    if(c.length == 2) {
     _cookies[c[0].match(re)[1]] = unescape(c[1].match(re) ? c[1].match(re)[1] : '');
    }
  }
}

function getCookie(name) {
  if(!_cookies) _initCookies();
  return _cookies[name];
}

function setCookie(name, value, days) {
  if(!_cookies) _initCookies();
  _cookies[name] = value;
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else var expires = "";
  path = path ? path : '/';
  var domain = location.host.match(/[^.]+\.[^.]+$/);
  document.cookie = name+"="+escape(value)+expires+"; path=" + path + (domain ? '; domain=.'+domain : ';');
}


