var approvalSwitch = false;

var number_of_checked_boxes = 0;
var current_submenu;
function init() { 
	if(document.getElementById('filter_controls'))	filterControls(); 
	if(document.getElementById('change_style'))	styles_changer(); 
	if(document.getElementById('filter_expand')) filter_changer();  
};
function filterControls(){  
	initiate_checkboxes();
	anchors = document.getElementById('filter_controls').getElementsByTagName('a');
	
	for(anchor in anchors)
	{  	
		if(anchors[anchor] != undefined){ 
			anchors[anchor].onclick=function() {  
				var checkbox = document.getElementById('checkbox_' + this.id);
				if(checkbox.checked == true){
					if(number_of_checked_boxes == 1){
						//alert('less than one filter');
						return false;
					}
					document.getElementById('checkbox_' + this.id).checked = false;  
					this.className = this.className.replace(new RegExp(" pressed\\b"), ""); 
					number_of_checked_boxes--; 
				}
				else{
					document.getElementById('checkbox_' + this.id).checked = true; 
					this.className += " pressed";
					number_of_checked_boxes++; 
				}
				checkbox.form.submit();
				return false;
			}		
		 }
	} 
}
function initiate_checkboxes(){
	anchors = document.getElementById('filter_controls').getElementsByTagName('a');
	
	for(anchor in anchors)
	{  	
		if(anchors[anchor].id != undefined){  
			var checkbox = document.getElementById('checkbox_' + anchors[anchor].id);
			if(checkbox.checked == true){
				anchors[anchor].className+=" pressed"; 	 
				number_of_checked_boxes++; 
			}
		}
	}
	if(number_of_checked_boxes < 1){
				anchors[0].className+=" pressed"; 			 
				document.getElementById('checkbox_' + anchors[0].id).checked = true;
				number_of_checked_boxes++; 
		}
}
function styles_changer(){
	var style_menu_btn = document.getElementById('change_style');
	var style_menu = document.getElementById('style_list');
	style_menu_btn.onmouseover=function() {
				document.getElementById('style_list').style.display = 'block';
			}
	style_menu_btn.onmouseout=function() {
				document.getElementById('style_list').style.display = 'none';
			}
	style_menu.onmouseover=function() {
				document.getElementById('style_list').style.display = 'block';
			}
	style_menu.onmouseout=function() {
				document.getElementById('style_list').style.display = 'none';
			}
}
function filter_changer(){ 
	document.getElementById('advaced_filter').style.display = 'none'; 
	document.getElementById('filter_expand').onclick=function() {
				if(document.getElementById('advaced_filter').style.display == 'none')
				{
					document.getElementById('advaced_filter').style.display = 'block';
					document.getElementById('filter_expand').style.backgroundPosition = '0 -30px';
				}else{
					document.getElementById('advaced_filter').style.display = 'none';
					document.getElementById('filter_expand').style.backgroundPosition = '0 0';
				}
				return false;
			} 
}
$(document).ready(function(){
	init();

  $("img").bind("contextmenu",function(e){
        return false;
  });

  $('a.tooltip').tooltip({
      track: true,
      delay: 0,
      showURL: false,
      showBody: " - ",
      fade: 250
  });
});

$(function() {
  applyLightBox();
});

function applyLightBox() {
  $('.lightbox').lightBox({fixedNavigation:true});

  var boxen = [];
  //find all links w/ rel="lightbox[gallery_name]" or just rel="lightbox" it works both ways
  jQuery('a[rel*=lightbox]').each(function() {
    //push only unique lightbox[gallery_name] into boxen array
    if (jQuery.inArray(jQuery(this).attr('rel'),boxen)) boxen.push(jQuery(this).attr('rel'));
  });
  
  //for each unique lightbox group, apply the lightBox
  jQuery(boxen).each(function(i,val) { jQuery('a[rel='+val+']').lightBox(); });
}

function showAbstractField(type) {
	var label;
	var action;
	var frm = document.forms['abstract_field_form'];

	switch (type)	{
		case 'friend':
			label = 'Request Friendship';
			action = 'friendship_request.php';
			break;

		case 'favorite':
			label = 'Add Favorite';
			action = 'favorites_add.php';
			break;

		case 'comment':
			label = 'Add Comment';
			action = 'profile_comment_new.php';
			break;

		case 'validate':
			label = 'Validate Profile';
			action = 'profile_validation_new.php';
			break;

		case 'note':
			label = 'Add Note';
			action = 'profile_note_new.php';
			break;

		default:
			label = 'Add Note';
			action = 'profile_note_new.php';
	}

	frm.action = action;
	frm.submit_button.value = label;
	frm.abstract_action.value = type;
	$('#abstract_field').fadeIn();
	$('#abstract_field').parent().css('height', 'auto');
}

$(window).load(function(){
  loc = String(location.href);
  if (loc.indexOf('#') >= 0) {  
    location_id = loc.substring(loc.indexOf('#') + 1); 
		if ($('#'+location_id).length > 0) {
			$.scrollTo( '-=221px');
		}
	}	
});



function getFlashMovie(movieName) {
  var isIE = navigator.appName.indexOf("Microsoft") != -1;
  return (isIE) ? window[movieName] : document[movieName];
}
function checkPhotoSession()
{
   return GetPhotoCookie('PHPSESSID')
}
function closeWindow()
{
  document.getElementById('tooltipWrapper').style.display = 'none';
  if (approvalSwitch) {
    document.location = '/profile.php?c=1';
  }
}
function GetPhotoCookie( check_name ) {
  // first we'll split this cookie up into name/value pairs
  // note: document.cookie only returns name=value, not the other components
  var a_all_cookies = document.cookie.split( ';' );
  var a_temp_cookie = '';
  var cookie_name = '';
  var cookie_value = '';
  var b_cookie_found = false; // set boolean t/f default f

  for ( i = 0; i < a_all_cookies.length; i++ )
  {
    // now we'll split apart each name=value pair
    a_temp_cookie = a_all_cookies[i].split( '=' );


    // and trim left/right whitespace while we're at it
    cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

    // if the extracted name matches passed check_name
    if ( cookie_name == check_name )
    {
      b_cookie_found = true;
      // we need to handle case where cookie has no value but exists (no = sign, that is):
      if ( a_temp_cookie.length > 1 )
      {
        cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
      }
      // note that in cases where cookie is initialized but no value, null is returned
      return cookie_value;
      break;
    }
    a_temp_cookie = null;
    cookie_name = '';
  }
  if ( !b_cookie_found )
  {
    return null;
  }
}
function showFlash(){	
    document.getElementById('tooltipWrapper').style.display = 'block';
    var flashvars = {};
    var params = {};
    var attributes = {name: "camShot",id: "camShot"};
    
    swfobject.embedSWF("CamShot.swf", "camShot", "870", "500",
"10.1","expressInstall.swf", flashvars, params, attributes);
}

function loadProfilePhotos() {
  window.setTimeout('loadProfilePhotosDelayed()', 1000);
}

function loadProfilePhotosDelayed() {
  if ($('#select_picture')) {
    var preLength = $('#select_picture a.lightbox').length;

    $.getJSON('/load_self_photos.php', function(data) {
      $('#select_picture').html(data);
      $('#select_picture a.lightbox').lightBox();

      if ($('a.lightbox').length >= 7) {
        $('#flashUploader').hide();
      }

      if (preLength == 0 && $('a.lightbox').length > 0) {
        approvalSwitch = true;
      }

      $('a.frontpage,a.delete').tooltip({
        track: true,
        delay: 0,
        showURL: false,
        showBody: " - ",
        fade: 250
      });
    });
  }
}

function hobbySubmit(type) {
  var hobby = $('#hobby'+type).val();
  var hobbyID = $('#hobby_sid'+type).val();

  $.post("hobby_add.php", { hobby_sid: hobbyID, hobby: hobby, type: type },
    function(data){
      $('#hobbies' + type).html(data);
    }
  );

  $('#hobby'+type).val('');
  $('#hobby_sid'+type).val(0);
}

function hobbyRemove(type, id, profile) {
  $.get('/hobby_remove.php?hobby_type_sid='+type+'&hobby_sid='+id+'&profile_sid='+profile, function(data) {
    $('#hobbies' + type).html(data);
  });

  return false;
}

function workingTimeRemove(id) {
  $.get('/working_time_remove.php?day_working_time_sid='+id, function(data) {
    $('#working_time').html(data);
  });

  return false;
}

function adminCommentRemove(id) {
  $.get('/admin_comment_remove.php?profile_admin_comment_sid='+id, function(data) {
    $('#admin_comments').html(data);
  });

  return false;
}


function showCJSPopup(options){
   var options = jQuery.extend({
     content : '',
     aftercontent : '',
     type : '',
     title : '',
     width : 500,
     submit : 0,
     url : 0,
     profile_sid : 0,
     height : 300
    }, options|| {});

    var template = '<div id="abstract_field" style="position: relative; display: block; "><form action="'+options.url+'" method="post" name="abstract_field_form"><textarea name="comment" style="width:215px" rows="6" cols="32"></textarea> <br><input type="submit" name="submit_button" value="'+options.submit+'" style="position: absolute; right: 0px"><input type="hidden" name="ajx" value="1"><input type="hidden" name="classType" value="'+options.type+'"><input type="hidden" name="profile_sid" value="'+options.profile_sid+'"><br></form></div>';

    var frame = '<div class="CJSPopupWrapper">';
    frame += '<div class="CJSPopupWrapperInner" style="width:'+options.width+'px;height:'+options.height+'px;">';
    if(options.title != '') frame += '<h1 class="ttl">'+options.title;
    frame += '<a href="#" class="close_subscribe"><img src="/images/close_cjspopup.png" alt="" /></a>';
    if(options.title != '') frame += '</h1>';
    frame += '<div class="CJSPopupWrapperContent">';
    frame += options.content;
    frame += template;
    frame += options.aftercontent;
    frame += '</div>';
    frame += '</div>';
    frame += '<a href="#" class="close_subscribe_from_bg"></a>';
    frame += '</div>';
    $('body').append(frame);

    $(".CJSPopupWrapper .close_subscribe_from_bg").click(function(e){
            e.preventDefault();
            e.stopPropagation();
            $(this).parent().remove();
    });

    $(".CJSPopupWrapper .close_subscribe").click(function(e){
            e.preventDefault();
            e.stopPropagation();
            if($(this).parent().hasClass("ttl")){
                    $(this).parent().parent().parent().remove();
            }else{
                    $(this).parent().parent().remove();
            }
    });
    return false;
}




(function($) {
  $.fn.outerHTML = function() {
    return $(this).clone().wrap('<div></div>').parent().html();
  }
})(jQuery);


/**
 * jQuery.ScrollTo - Easy element scrolling using jQuery.
 * Copyright (c) 2007-2009 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
 * Dual licensed under MIT and GPL.
 * Date: 5/25/2009
 * @author Ariel Flesler
 * @version 1.4.2
 *
 * http://flesler.blogspot.com/2007/10/jqueryscrollto.html
 */
;(function(d){var k=d.scrollTo=function(a,i,e){d(window).scrollTo(a,i,e)};k.defaults={axis:'xy',duration:parseFloat(d.fn.jquery)>=1.3?0:1};k.window=function(a){return d(window)._scrollable()};d.fn._scrollable=function(){return this.map(function(){var a=this,i=!a.nodeName||d.inArray(a.nodeName.toLowerCase(),['iframe','#document','html','body'])!=-1;if(!i)return a;var e=(a.contentWindow||a).document||a.ownerDocument||a;return d.browser.safari||e.compatMode=='BackCompat'?e.body:e.documentElement})};d.fn.scrollTo=function(n,j,b){if(typeof j=='object'){b=j;j=0}if(typeof b=='function')b={onAfter:b};if(n=='max')n=9e9;b=d.extend({},k.defaults,b);j=j||b.speed||b.duration;b.queue=b.queue&&b.axis.length>1;if(b.queue)j/=2;b.offset=p(b.offset);b.over=p(b.over);return this._scrollable().each(function(){var q=this,r=d(q),f=n,s,g={},u=r.is('html,body');switch(typeof f){case'number':case'string':if(/^([+-]=)?\d+(\.\d+)?(px|%)?$/.test(f)){f=p(f);break}f=d(f,this);case'object':if(f.is||f.style)s=(f=d(f)).offset()}d.each(b.axis.split(''),function(a,i){var e=i=='x'?'Left':'Top',h=e.toLowerCase(),c='scroll'+e,l=q[c],m=k.max(q,i);if(s){g[c]=s[h]+(u?0:l-r.offset()[h]);if(b.margin){g[c]-=parseInt(f.css('margin'+e))||0;g[c]-=parseInt(f.css('border'+e+'Width'))||0}g[c]+=b.offset[h]||0;if(b.over[h])g[c]+=f[i=='x'?'width':'height']()*b.over[h]}else{var o=f[h];g[c]=o.slice&&o.slice(-1)=='%'?parseFloat(o)/100*m:o}if(/^\d+$/.test(g[c]))g[c]=g[c]<=0?0:Math.min(g[c],m);if(!a&&b.queue){if(l!=g[c])t(b.onAfterFirst);delete g[c]}});t(b.onAfter);function t(a){r.animate(g,j,b.easing,a&&function(){a.call(this,n,b)})}}).end()};k.max=function(a,i){var e=i=='x'?'Width':'Height',h='scroll'+e;if(!d(a).is('html,body'))return a[h]-d(a)[e.toLowerCase()]();var c='client'+e,l=a.ownerDocument.documentElement,m=a.ownerDocument.body;return Math.max(l[h],m[h])-Math.min(l[c],m[c])};function p(a){return typeof a=='object'?a:{top:a,left:a}}})(jQuery);
