﻿$(document).ready(function() {
	
	//Check if this device is VGA
	var isVGA = false;
	if (_SF_Constants.vga) {
		isVGA = true;
	}
	
	//Handle hi_res using screen widths and vga
	var bodyWidth = $('body').width();
	if (typeof bodyWidth == 'number') {
		if ((bodyWidth > 400) && isVGA) {
			$('body').addClass('hi_res');
		}
	}
	
	$(".btn-download, .link-download").click(function(e) {
		
		//Prevent event so we can initiate tracking 1st
		e.preventDefault();
		var isAnchor = (e.target.tagName == "A");
		var isParentAnchor = ($(this).parent('a').length > 0);
		var isFormSubmit = ($(this).attr('type') == 'image');
		var finalLocation = '#';

		var phoneType = '';
		var currentPage = '';
		
		if (isFormSubmit) {
			phoneType = $('form input:radio[name=phone_type]:checked').attr('value');
			currentPage = $('form').attr('page_name');
		} else if (isParentAnchor || isAnchor) {
			if (isParentAnchor) {
				currentPage = $(this).parent('a').attr('page_name');
				finalLocation = $(this).parent('a').attr('href');
			} else if (isAnchor) {
				currentPage = $(this).attr('page_name');
				finalLocation = $(this).attr('href');
			}
			if (finalLocation.indexOf('Skyfire-S60') > -1) {
				phoneType = 'sym_no_touch';
			} else if (finalLocation.indexOf('Skyfire-S60-5th') > -1) {
				phoneType = 'sym_touch';
			} else if (finalLocation.indexOf('Skyfire-PPC') > -1) {
				phoneType = 'win_touch';
			} else if (finalLocation.indexOf('Skyfire-SMP') > -1) {
				phoneType = 'win_no_touch';
			}
		}
		
		if (!phoneType) {
			phoneType = 'unknown';
			return false;
		}
		if (!currentPage) {
			currentPage = 'unknown';
		}
		
		//Setup Google Event Str
		var googleEventDataStr = 'downloadviamobile';
		if (_SF_Constants.cid) {
			googleEventDataStr += '-' + _SF_Constants.cid;
		}
		
		//Fire Google tracking
		try {
			pageTracker._trackEvent('getskyfire_m', googleEventDataStr, phoneType);
		} catch(err) {
		}
		
		if (isFormSubmit) {
			$('form').submit();
		} else {
			window.location = finalLocation;
		}
		
	});
	
});

